如何在C#中创建Excel的字典

发布于 2025-01-20 21:20:26 字数 862 浏览 0 评论 0原文

我有像这样的excel:

productidsoming shosexplanationshotercolumn1另一个column2另一个column3
1x6a65465
2y5b6556
3z7c65465

我想创建词典,该字典是键值(这是progienceid as progucateid as Productid,sopplantranation,soseexplanation,sothercolumn1,sherecolumn2,eytercolumn2,eytercolumn2,又是另一个dictifty3)必须具有值列表(例如字典键:productid及其值:1,2,3等),我认为必须有包含所有字典的列表。

我正在使用Aspose库作为Excel和.NET Framework 4.5。

Aspose将其单元格值返回为对象。

因此,我的第一个问题如何创建字典列表,这些词典必须具有值列表(list< dictionary< key,值列表>>),以及如何将值添加到此字典列表中?

我的第二个问题:如何用Aspose工作表填写此词典列表?

I have excel like this :

ProductIDSomeExplanationAnotherColumn1AnotherColumn2AnotherColumn3
1X6A65465
2Y5B6556
3Z7C65465

I want to create Dictionary that key values(which are ProductID, SomeExplanation,AnotherColumn1,AnotherColumn2, AnotherColumn3) and this dictionary must have List of values (for example dictionary key : ProductId and it's values : 1,2,3 etc..) and I think there must be List that containes all dictionaries.

I am using aspose library for excel and .net framework 4.5 .

Aspose returning the it's cell values as an object.

So my first question how can create List of dictionaries, and these dictionaries must have list of values (List<Dictionary<key,List of values>>) and how to add values to this List of dictionary ?

My second question with that : how can I fill this list of dictionaries with aspose worksheet ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-01-27 21:20:26

这是接受 Aspose Worksheet 作为参数的方法,该工作表参数可以是 Excel 文件的工作表之一。

我想遍历所有单元格并将值分配给字典,并且该值属于其标题(0行和列顺序)

例如:有一个名为myExcelContainer的列表,该列表是一系列Excel columns 并且此列是一个字典,其中包含值键(Excel 标题 - 例如 ProductId)以及 Excel 标题下的值 [1, 2, 3]。

 public List<Dictionary<string, List<object>>> GenerateExcelDictionary(Worksheet worksheet)
        {
            var columnMax = worksheet.Cells.MaxDataColumn;
            var rowMax = worksheet.Cells.MaxDataRow;
            var myExcelContainer = new List<Dictionary<string, List<object>>>();
            var columnKeyWithValues = new Dictionary<string, List<object>>();
            for (int column = 0; column < columnMax; column++)
            {
                var columnName = worksheet.Cells[0, column].Value.ToString().Replace(" ", string.Empty);
                columnKeyWithValues.Add(columnName, new List<object>());
            }

            for (int column = 0; column < columnMax; column++)
            {
                var values = new List<object>();
                for (int row = 1;row < rowMax;row++)
                {
                    values.Add(worksheet.Cells[row, column]);
                }
                columnKeyWithValues[worksheet.Cells[0, column].Value.ToString()] = values;
            }
            myExcelContainer.Add(columnKeyWithValues);
            return myExcelContainer;
        }

这是 Excel 容器:

var myExcelContainer = new List<Dictionary<string, List<object>>>();

但是如果您可以提高我希望您分享的算法性能,请。

我的英语不太好:)。

This is method that accept Aspose Worksheet as a parameter this worksheet parameter can be one of the excel files' worksheet.

I want to iterate through all cell and assign values to dictionary, and this values belong to its header(0 row and columnOrder)

For example: there is a list called myExcelContainer and this list is a series of Excel columns and also this columns is an dictionary that contains key of value (Excel header - for example ProductId) and the values [1, 2, 3] under the Excel header.

 public List<Dictionary<string, List<object>>> GenerateExcelDictionary(Worksheet worksheet)
        {
            var columnMax = worksheet.Cells.MaxDataColumn;
            var rowMax = worksheet.Cells.MaxDataRow;
            var myExcelContainer = new List<Dictionary<string, List<object>>>();
            var columnKeyWithValues = new Dictionary<string, List<object>>();
            for (int column = 0; column < columnMax; column++)
            {
                var columnName = worksheet.Cells[0, column].Value.ToString().Replace(" ", string.Empty);
                columnKeyWithValues.Add(columnName, new List<object>());
            }

            for (int column = 0; column < columnMax; column++)
            {
                var values = new List<object>();
                for (int row = 1;row < rowMax;row++)
                {
                    values.Add(worksheet.Cells[row, column]);
                }
                columnKeyWithValues[worksheet.Cells[0, column].Value.ToString()] = values;
            }
            myExcelContainer.Add(columnKeyWithValues);
            return myExcelContainer;
        }

this is the excel container :

var myExcelContainer = new List<Dictionary<string, List<object>>>();

But If you can improve the algortihm performance I want you to share, please.

My english not great :) .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文