ExcelPackage和98千行

4
我想使用CodePlex ExcelPackage写一个Excel文件(2007),但是写入Excel文件需要很长时间。我没有找到任何可以接受数据源的方法。
我的代码片段如下:
var newFile = new FileInfo(GlobalVariables.Compare2007Path);

using (var excelpackage = new ExcelPackage(newFile))
{
  var myWorkbook = excelpackage.Workbook;
  myWorkbook.Worksheets.Add("sheetname");

  var xlWorkSheet = xlWorkBook.Worksheets["sheetname"];

  //loop the data and fill the columns
  var rowCount = 2;
  foreach (var compare in objCompare)
  {
    xlWorkSheet.Cell(rowCount, 1).Value = compare.adserverIdSite.ToString();
    xlWorkSheet.Cell(rowCount, 2).Value = compare.site;
    xlWorkSheet.Cell(rowCount, 3).Value = compare.adserverIdZone.ToString();
    xlWorkSheet.Cell(rowCount, 4).Value = compare.zone;
    xlWorkSheet.Cell(rowCount, 5).Value = compare.position;
    xlWorkSheet.Cell(rowCount, 6).Value = compare.weekday;
    xlWorkSheet.Cell(rowCount, 7).Value = compare.oldimps.ToString();
    xlWorkSheet.Cell(rowCount, 8).Value = compare.olduu.ToString();
    xlWorkSheet.Cell(rowCount, 9).Value = compare.oldimpsuu.ToString();
    xlWorkSheet.Cell(rowCount, 10).Value = compare.newimps.ToString();
    xlWorkSheet.Cell(rowCount, 11).Value = compare.newuu.ToString();
    xlWorkSheet.Cell(rowCount, 12).Value = compare.newimpsuu.ToString();
    xlWorkSheet.Cell(rowCount, 13).Value = compare.diffimps.ToString();
    xlWorkSheet.Cell(rowCount, 14).Value = compare.diffimpsperc.ToString();
    rowCount++;
  }
  excelpackage.Save();
}

除了excelpackage,还有其他选项吗?

3个回答

7
我找到了解决Excel包性能问题的方法。你需要在Excel包上应用一个补丁。这个补丁可以在这里找到。请查找id:1042或更新1233(该补丁具有更多功能)。
有了这个补丁,你可以在空表格上添加数据表。添加98000条记录和14列数据只需几秒钟。

1
我按照这个教程使用ExcelPackage:http://excelpackage.codeplex.com/wikipage?title=Using%20a%20template%20to%20create%20an%20Excel%20spreadsheet(对于大数据,sqlreader... while.read使其变得非常缓慢)。我应用了上面建议的补丁(1233),并使用了新的LoadFromDataTable方法和SqlDataAdapter代替sqlreader,如此处所建议:https://dev59.com/PlzUa4cB1Zd3GeqP2Vcz。我获得了巨大的性能提升(从“超时”服务器错误到“现在它可以在几秒钟内运行!”)。 - firepol

1

SpreadsheetGear for .NET 可以做到。

您可能想查看使用 SpreadsheetGear 预装的 SpreadsheetGear Explorer 示例解决方案(适用于 C# 或 VB)。在高级选项下有一个示例,展示了快速填充单元格的方法。在我的机器上(超频 Intel QX6850),它可以在 0.05 秒内创建 50,000 行、4 列。

您可以 在此处 下载免费试用版,这将安装 SpreadsheetGear 组件和上述提到的 SpreadsheetGear Explorer 示例。

声明:我拥有 SpreadsheetGear LLC。


1
+1 我也遇到了使用 excelpackage 在前几百行后无法很好地扩展的问题,因为它使用了 Xpath。尝试在 codeplex 上添加补丁,但对我没有改善。我重构了使用 spreadsheetgear 2010 演示版,速度快得多,并且在创建和读取方面呈线性扩展。我们很快就会购买完整版。 - Dale

0

我曾经使用(并购买了)SmartXL来创建Excel文件。它不是免费的,但节省了我很多时间。你可以免费试用30天。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接