简体   繁体   English

将数据导出到Excel电子表格

[英]Exporting data to an excel spreadsheet

I am going to be working on a general purpose toolkit for writing excel files based off a database data. 我将要开发一个通用工具箱,用于基于数据库数据编写excel文件。 I've worked with a couple of methods before: 我之前使用过几种方法:

1) Create a CSV file 1)创建一个CSV文件
Excel will open it and format the data with each value going into a single cell, each line is one row. Excel将打开它并格式化数据,每个值进入单个单元格,每一行为一行。
Drawbacks: file extension is not always going to be opened by excel by default. 缺点:默认情况下,Excel不一定总是打开文件扩展名。
It seems to 'cut off' on very large data sources. 它似乎“切断”了非常大的数据源。 From memory it hit around 65,000 rows before the rest of the file was 'ignored' by excel. 在内存中,它击中了大约65,000行,然后文件的其余部分被excel“忽略”。

2) Generate an XML spreadsheet 2)生成XML电子表格
http://msdn.microsoft.com/en-us/library/bb226687(v=office.11).aspx http://msdn.microsoft.com/zh-CN/library/bb226687(v=office.11​​).aspx
Drawbacks: Error's when opening 'the file you are trying to open ** is in a different format than specified by the file extension'. 缺点:打开“您尝试打开的文件**的格式与文件扩展名指定的格式不同”时出错。 It does open fine, but it doesn't inspire confidence. 它确实可以打开,但是并不能激发信心。
Makes writing SQL-to-XML awkward. 使编写SQL到XML变得笨拙。

I briefly looked at using the excel Interop methods a while ago. 不久前,我简要介绍了使用excel Interop方法的情况。 And I am sure I had issues when trying to run the code from a server which doesn't have excel/office installed. 而且我确信尝试从未安装excel / office的服务器运行代码时遇到问题。 This will almost always be the case . 几乎总是这样

Generally the code will be run either through a webpage or a silverlight WCF service (if that makes a difference). 通常,代码将通过网页或Silverlight WCF服务运行(如果有所不同)。
And either emailed to a user, or would also be nice to have the option to just open the file straight away and allow them to save. 既可以通过电子邮件发送给用户,也可以选择直接打开文件并允许他们保存。
Including headers/colours/formula's would be a bonus 包括标题/颜色/公式将是一个奖励

Mostly at the moment they would just copy/paste the data. 目前大多数情况下,他们只是复制/粘贴数据。
But with a large data set it is prone to failure, or is just really really slow. 但是,如果数据集很大,则很容易发生故障,或者速度真的很慢。

Anyone give me the best ideas how to go about it 任何人都给我最好的主意如何去做

You could use a commerical product, such as Aspose.Cells to generate the Excel spreadsheet in C# on the server without having Office installed. 您可以使用诸如Aspose.Cells之类的商业产品在服务器上以C# 格式生成Excel电子表格,而无需安装Office。 They have some sample code that generates a spreadsheet in an aspx.cs page, if you want to see what the object model looks like. 如果您想查看对象模型的外观,他们有一些示例代码可在aspx.cs页面中生成电子表格。

Unfortunately, their pricing scheme is really awkward if you have more than one developer who gets the code from source control and builds it; 不幸的是,如果您有多个开发人员从源代码​​管理中获取代码并进行构建,那么他们的定价方案确实很尴尬。 they officially consider you to be part of the same team and require licenses for each developer (even if you're the only programmer who directly works on that code). 他们正式认为您是同一团队的一员,并要求每个开发人员获得许可(即使您是直接使用该代码的唯一程序员)。

Since CSV has the problem of cutting off after big number of rows, that approach is out. 由于CSV存在在大量行之后中断的问题,因此该方法已被淘汰。

Using Interop is tricky. 使用Interop非常棘手。 In addition to ensuring that you have Excel and PIA installed on the server, you need to run the Excel code in a single thread (using some kind of queuing mechanism). 除了确保在服务器上安装了Excel和PIA之外,还需要在单个线程中运行Excel代码(使用某种排队机制)。 Otherwise, you will start running into issues on that front. 否则,您将开始遇到该方面的问题。

The XML actually seems to be the most reliable solution, save for the confusing message. 除了令人困惑的消息外,XML实际上似乎是最可靠的解决方案。 If you can answer my question in the comments, we might be able to assist you with that, as well. 如果您可以在评论中回答我的问题,我们也可以为您提供帮助。

PS. PS。 If you can determine the number of rows before generating the Excel sheet (which you should), you could deliver the results in one of the 2 formats: CSV for small files, XML for large, the trade-off being the confusing message (provided that it won't go away). 如果您可以在生成Excel工作表之前确定行数(应该这样做),则可以采用以下两种格式之一提供结果:CSV用于小文件,XML用于大文件,折衷方案是令人困惑的消息(已提供)它不会消失)。 Depending on the percentage of files being over the CSV threshold, that might be an acceptable compromise. 根据超出CSV阈值的文件百分比,这可能是可以接受的折衷方案。

I recommend using EPPlus , it is very easy to convert DataTable s to Excel files and allows for tons of styling options and even data validation. 我建议使用EPPlus ,将DataTable转换为Excel文件非常容易,并且允许大量的样式选择甚至数据验证。 You don't even need Excel installed on the server. 您甚至不需要在服务器上安装Excel。

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

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