【问题标题】:Generate Pivot Table in CSV Dynamically from Dictionary with Documentformat.Openxml使用 Documentformat.Openxml 从字典动态生成 CSV 数据透视表
【发布时间】:2021-09-02 16:06:11
【问题描述】:

我正在使用Documentformat.Openxml

[https://docs.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet?view=openxml-2.8.1]

Dictionary<string, List<string>> 对象动态生成CSV 中的数据透视表,该对象的结构类似于:

我在这里关注Sample 来创建应该看起来有点像的表:

但不确定如何从数据字典创建表。

我当前的代码sn-p:

// Add a WorkbookPart to the document.  
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.  
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.  
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());

            // Append a new worksheet and associate it with the workbook.  
            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };

            sheets.Append(sheet);
            Worksheet worksheet = new Worksheet();
            SheetData sheetData = new SheetData();
            Row row = new Row();
            Cell cell = new Cell()
            {
                CellReference = "A1",
                DataType = CellValues.String,
                CellValue = new CellValue("Microsoft")
            };
            row.Append(cell);
            sheetData.Append(row);
            worksheet.Append(sheetData);
            worksheetPart.Worksheet = worksheet;

            // Close the document.  
            spreadsheetDocument.Close();

在上面的 sn-p 中,您可以看到我正在对单元格值进行硬编码,但实际上我想从字典中动态填充它,并且其中有大量数据。做这个的最好方式是什么?有人可以帮忙吗?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    正如您的问题所示,您想使用 Open XML 生成 excel 文件。你应该看看这些答案

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 2013-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多