c# - 如何将大量数据导出到 excel

标签 c# performance excel memory export

我目前正在使用 EPPlus 将数据导出到 excel。它对少量数据非常有效。但是导出大量数据会消耗大量内存。

我简要介绍了 OOXML 和/或 Microsoft Open XML SDK 2.5。我不确定是否可以使用它来将数据导出到 Excel?

还有第三方提供程序库。

我想知道什么解决方案可以以良好的性能正确地导出大量数据并且不占用太多空间(理想情况下少于导出数据量的 3 倍)?

Update: some extra requirements... I need to be able to export "color" information (that exclude CSV) and I would like something easy to manage like EPPlus library (exclude the XML format itself). I found another thread and they recommend Aspose or SpreadsheetGear which I'm trying. I put first answer as ok. Thanks to all.

更新 2016-02-16 正如信息...我们现在使用 SpreadSheetGear,我们喜欢它。我们曾经需要支持,这很棒。

谢谢

最佳答案

EPPlus to export data to excel. It works admirably for small amount of data. But it consume a lots of memory for large amount of data to export.

几年前,我写了一个 C# 库,使用 OpenXML 库将数据导出到 Excel,我遇到了同样的情况。

在您开始拥有大约 30k+ 行之前,它工作正常,此时,库将尝试缓存您的所有数据......并且它会耗尽内存。

但是,我通过使用 OpenXmlWriter 解决了这个问题。类(class)。这会将数据直接写入 Excel 文件(无需先缓存)并且内存效率更高。

而且,正如您将看到的,该库非常易于使用,只需调用一个 CreateExcelDocument函数,并传递给它 DataSet , DataTableList<> :

// Step 1: Create a DataSet, and put some sample data in it
DataSet ds = CreateSampleData();

// Step 2: Create the Excel .xlsx file
try
{
    string excelFilename = "C:\\Sample.xlsx";
    CreateExcelFile.CreateExcelDocument(ds, excelFilename);
}
catch (Exception ex)
{ 
    MessageBox.Show("Couldn't create Excel file.\r\nException: " + ex.Message);
    return;
}

您可以从此处下载 C# 和 VB.Net 的完整源代码:

Mike's Export to Excel

祝你好运!

关于c# - 如何将大量数据导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100900/

相关文章:

c# - StreamGeometry 与 WPF C# 中的 DrawingContext.DrawLine

r - 从 Excel 中将 float 作为字符串导入时出现问题。不正确地添加精度

c# - 如何从字符串动态运行比较?

c# - CLI 和 GUI 应用程序

c# - Azure 函数从大 JSON 格式到 SQL

python - Cython 是否提供任何合理简单有效的方法来迭代 Numpy 数组,就好像它们是扁平的一样?

Android 默认线程及其使用

c# - 在 .NET 中操作 Excel xls 和 xlsx

excel - 通过 excel 中的列循环 DateLastModified 函数

c# - "Type is not expected, and no contract can be inferred: Leap.Vector"错误。使用 Protobuf-net 序列化器