简体   繁体   English

如何将大量数据导出到excel

[英]How can I export very large amount of data to excel

I'm currently using EPPlus to export data to excel.我目前正在使用 EPPlus 将数据导出到 excel。 It works admirably for small amount of data.它适用于少量数据。 But it consume a lots of memory for large amount of data to export.但是要导出大量数据会消耗大量内存。

I've briefly take a look at OOXML and/or the Microsoft Open XML SDK 2.5.我简要介绍了 OOXML 和/或 Microsoft Open XML SDK 2.5。 I'm not sure I can use it to export data to Excel?我不确定我可以用它来将数据导出到 Excel 吗?

There is also third party provider libraries.还有第三方提供程序库。

I wonder what solution could do the job properly of exporting very large amount of data in good performance and not taking to much spaces (ideally less than 3x the amount of data to export) ?我想知道什么解决方案可以正确地以良好的性能导出大量数据并且不占用太多空间(理想情况下少于要导出的数据量的 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).更新:一些额外的要求......我需要能够导出“颜色”信息(不包括 CSV),我想要一些易于管理的东西,比如 EPPlus 库(不包括 XML 格式本身)。 I found another thread and they recommend Aspose or SpreadsheetGear which I'm trying.我找到了另一个线程,他们推荐了我正在尝试的 Aspose 或 SpreadsheetGear。 I put first answer as ok.我把第一个答案作为确定。 Thanks to all.谢谢大家。

Update 2016-02-16 Just as information... We now use SpreadSheetGear and we love it.更新 2016-02-16就像信息一样......我们现在使用 SpreadSheetGear,我们很喜欢它。 We required support once and it was awesome.我们曾经需要支持,这很棒。

Thanks谢谢

EPPlus to export data to excel. EPPlus 将数据导出到 excel。 It works admirably for small amount of data.它适用于少量数据。 But it consume a lots of memory for large amount of data to export.但是要导出大量数据会消耗大量内存。

A few years ago, I wrote a C# library to export data to Excel using the OpenXML library, and I faced the same situation.几年前,我写了一个C#库,使用OpenXML库将数据导出到Excel,遇到了同样的情况。

It worked fine until you started to have about 30k+ rows, at which point, the libraries would be trying to cache all of your data... and it'd run out of memory.它运行良好,直到您开始有大约 30k+ 行,此时,库将尝试缓存您的所有数据......并且它会耗尽内存。

However, I fixed the problem by using the OpenXmlWriter class.但是,我通过使用OpenXmlWriter类解决了这个问题。 This writes the data directly into the Excel file (without caching it first) and is much more memory efficient.这会将数据直接写入 Excel 文件(无需先缓存),并且内存效率更高。

And, as you'll see, the library is incredibly easy to use, just call one CreateExcelDocument function, and pass it a DataSet , DataTable or List<> :而且,正如您将看到的,该库非常易于使用,只需调用一个CreateExcelDocument函数,并将其传递给DataSetDataTableList<>

// 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;
}

You can download the full source code for C# and VB.Net from here:您可以从这里下载 C# 和 VB.Net 的完整源代码:

Mike's Export to ExcelMike 的导出到 Excel

Good luck !祝你好运!

If your requirements are simple enough, you can just use CSV.如果您的要求足够简单,则可以使用 CSV。

If you need more detail, look into SpreadsheetML .如果您需要更多详细信息,请查看SpreadsheetML It's an XML schema that you can use to create a text document that Excel can open natively.它是一种 XML 架构,可用于创建 Excel 可以本机打开的文本文档。 It supports formulas, multiple worksheets per workbook, formatting, etc.它支持公式、每个工作簿的多个工作表、格式等。

I second using CSV but note that Excel has limits to the number of rows and columns in a worksheet as described here: http://office.microsoft.com/en-us/excel-help/excel-specifications-and-limits-HP010342495.aspx我第二次使用 CSV,但请注意 Excel 对工作表中的行数和列数有限制,如下所述: http : //office.microsoft.com/en-us/excel-help/excel-specifications-and-limits- HP010342495.aspx

specifically: Worksheet size 1,048,576 rows by 16,384 columns特别是:工作表大小 1,048,576 行 x 16,384 列

This is for Excel 2010. Keep these limits in mind when working with very large amounts of data.这是针对 Excel 2010 的。在处理大量数据时,请记住这些限制。

As an alternative you can use my SwiftExcel library.作为替代方案,您可以使用我的SwiftExcel库。 It was design for high volume Excel output that writes data directly to the file with no memory impact.它专为高容量 Excel 输出而设计,可将数据直接写入文件而不会影响内存。

Here is a sample of usage:下面是一个使用示例:

using (var ew = new ExcelWriter("C:\\temp\\test.xlsx"))
{
    for (var row = 1; row <= 100; row++)
    {
        for (var col = 1; col <= 10; col++)
        {
            ew.Write($"row:{row}-col:{col}", col, row);
        }
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM