What is the best / fastest way to export large set of data from C# to excel

时光毁灭记忆、已成空白 提交于 2019-12-01 05:57:31

问题


I have code that uses the OpenXML library to export data.

I have 20,000 rows and 22 columns and it takes ages (about 10 minutes).

is there any solution that would export data from C# to excel that would be faster as i am doing this from an asp.net mvc app and many people browsers are timing out.


回答1:


Assuming 20'000 rows and 22 columns with about 100 bytes each, makes 41 megabytes data alone. plus xml tags, plus formatting, I'd say you end up zipping (.xlsx is nothing but several zipped xml files) 100 mb of data.

Of course this takes a while, and so does fetching the data. I recommend you use excel package plus instead of the Office OpenXML development kit. http://epplus.codeplex.com/

There's probably a bug/performance-issue in the write-in-a-hurry-and-hope-that-it-doesnt-blow-up-too-soon Microsoft code.




回答2:


CSV. It is a plain text file, but can be opened by any version of Excel.

No doubt it is a easier way to export data to excel. A lot of website provide data export as CSV.

What you need to do is just add a comma (,) to separate the values and a line break to separate the records. It won't take extra resource to build the csv file, so it is quite fast.




回答3:


I wound up using an open source solution called ClosedXML that worked great




回答4:


Depending on what version of Excel you are targetting, you could expose the data as an OData service which Excel 2010 can naturally consume and will handle the downloading and formattting for you.




回答5:


I am assuming that this data is something that needs to be completely sent to the client and has already been pre-filtered in some fashion, but still needs to be sent back to the person who made the request.

In this case, you want to perform this particular operation 'asynchronously'. I'm not sure if this would fit your workflow, but say that a person requests this large XML formatted document, I would: a) queue another worker thread to kick off the generation of the document while returning a 'token' (perhaps a GUID to the requester); b) return a link to a page where the requestor can click on the link (passing the token) allowing the page to look up results.

If the thread has completed processing the document, it places it into a special folder with a unique name and adds the token to a database table with its document location. If the person requests that page, the token exists in the database and the document exists on the file system, they are allowed to click and download it through HTTP. If it does not exist, they are either told it does not exist or to wait for the results. (This message can be based on the time the request was received.)

If the person downloads the document successfully (and you can do this through script), you can remove the entry for the database for the document with that token and delete the file from the file system.

I hope I read this question correctly.




回答6:


I have found that I can speed up exporting data from a database into an Excel spreadsheet by limiting the number of export operations. I found that by accumulating 100 lines of data before writing, the creation speed increased by a factor of at least 5-10x.




回答7:


The mistake when exporting data that is most often done when exporting data is in the workflow

  • Build Model
  • Build XML DOM
  • Save XML DOM to file

This workflow leads to an overhead because building up the XML DOM needs it's time, the XML DOM is kept in memory together with the Model and then the whole bunch of data is written to a file.

A better way to handle this is to convert your model entry by entry directly to the target format and write it directly to a (buffered) file.

A format with low overhead that's fast to write and is readable by Excel is CSV (ok, it's legacy, it's awkward...).



来源:https://stackoverflow.com/questions/7844466/what-is-the-best-fastest-way-to-export-large-set-of-data-from-c-sharp-to-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!