gpt4 book ai didi

c# - .NET OpenXML 性能问题

转载 作者:太空狗 更新时间:2023-10-29 17:36:01 28 4
gpt4 key购买 nike

我正在尝试使用 OpenXML 从 ASP.NET Web 服务器写出一个 Excel 文件。我有大约 2100 条记录,大约需要 20-30 秒才能完成。有什么办法可以让它更快吗?从数据库中检索 2100 行需要几分之一秒。不确定为什么在内存中操作它们会花费更长的时间。

注意:ExcelWriter 是我们的自定义类,但它的所有方法都直接来自此链接中的代码,http://msdn.microsoft.com/en-us/library/cc861607.aspx

   public static MemoryStream CreateThingReport(List<Thing> things, MemoryStream template)
{
SpreadsheetDocument spreadsheet = SpreadsheetDocument.Open(template, true);
WorksheetPart workSheetPart = spreadsheet.WorkbookPart.WorksheetParts.First();

SharedStringTablePart sharedStringPart = spreadsheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First();

Cell cell = null;
int index = 0;

//create cell formatting for header text
Alignment wrappedAlignment = new Alignment { WrapText = true };
uint rowOffset = 2;

foreach (Thing t in things)
{
//Received Date
cell = ExcelWriter.InsertCellIntoWorksheet("A", rowOffset, workSheetPart);
index = ExcelWriter.InsertSharedStringItem(t.CreateDate.ToShortDateString(), sharedStringPart);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.SharedString);

//Car Part Name
cell = ExcelWriter.InsertCellIntoWorksheet("B", rowOffset, workSheetPart);
index = ExcelWriter.InsertSharedStringItem(t.CarPart.Name, sharedStringPart);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.SharedString);

rowOffset++;
}

workSheetPart.Worksheet.Save();

spreadsheet.WorkbookPart.Workbook.Save();
spreadsheet.Close();

return template;

最佳答案

所以看起来 MSDN 社区文档中的某个人遇到了类似的性能影响。下面的代码非常低效。有人推荐使用哈希表。

对于我们的解决方案,我们完全删除了共享字符串的插入,下载时间从 1:03 秒减少到 0:03 秒。

//Old: (1:03)
cell = ExcelWriter.InsertCellIntoWorksheet("A", rowOffset, workSheetPart);
index = ExcelWriter.InsertSharedStringItem(thing.CreateDate.ToShortDateString(), sharedStringPart);
cell.CellValue = new CellValue(index.ToString());
cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.SharedString);

//New: (0:03)
cell = ExcelWriter.InsertCellIntoWorksheet("A", rowOffset, workSheetPart);
cell.CellValue = new CellValue(thing.CreateDate.ToShortDateString());
cell.DataType = new DocumentFormat.OpenXml.EnumValue<CellValues>(CellValues.String);

MSDN Docs(缓慢的解决方案,他们应该改用哈希表)

      private static int InsertSharedStringItem(string text, SharedStringTablePart         shareStringPart)
{
// If the part does not contain a SharedStringTable, create one.
if (shareStringPart.SharedStringTable == null)
{
shareStringPart.SharedStringTable = new SharedStringTable();
}

int i = 0;

// Iterate through all the items in the SharedStringTable. If the text already exists, return its index.
foreach (SharedStringItem item in shareStringPart.SharedStringTable.Elements<SharedStringItem>())
{
if (item.InnerText == text)
{
return i;
}

i++;
}

// The text does not exist in the part. Create the SharedStringItem and return its index.
shareStringPart.SharedStringTable.AppendChild(new SharedStringItem(new DocumentFormat.OpenXml.Spreadsheet.Text(text)));
shareStringPart.SharedStringTable.Save();

return i;
}

关于c# - .NET OpenXML 性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10542719/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com