我目前正在使用 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
, DataTable
或 List<>
:
// 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 的完整源代码:
祝你好运!
关于c# - 如何将大量数据导出到 excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19100900/