【发布时间】: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 中,您可以看到我正在对单元格值进行硬编码,但实际上我想从字典中动态填充它,并且其中有大量数据。做这个的最好方式是什么?有人可以帮忙吗?
【问题讨论】: