简体   繁体   English

通过C#Web服务将大量数据从SQL Server流式传输到Excel文件

[英]Streaming large amounts of data from sql server to an Excel file via c# web service

So I'm trying to get the results from a stored proc (200k rows+) into an Excel file from ASP.NET but having a few difficulties. 因此,我试图从存储的proc(200k行以上)中将结果从ASP.NET获取到Excel文件中,但遇到了一些困难。 I don't think csv is an option as the client want the numbers formatted correctly. 我不认为csv是一种选择,因为客户端希望数字格式正确。 I've tried three third party Excel libraries but all have fallen over with so much data and are using gigabytes of memory. 我已经尝试了三个第三方Excel库,但是所有库都掉了这么多数据,并使用了千兆字节的内存。

I've wrote some code to generate an Excel XML file and it runs very quickly but the file is over 300megs. 我已经写了一些代码来生成Excel XML文件,它运行非常快,但是文件超过300兆。 If I open and save as a native Excel file it gets it down to 30megs. 如果我打开并另存为本机Excel文件,它将降低到30megs。 At the moment my best solution is to zip this xml file on the server which gets it down to 7megs but the user is still going to end up with a huge file once unzipped. 目前,我最好的解决方案是将这个xml文件压缩到服务器上,将其压缩到7兆像素,但是一旦解压缩,用户仍然会得到一个巨大的文件。 Ideally I'd like to find a third party Excel library that can write a native Excel file with 200,000+ rows without killing the server, any ideas? 理想情况下,我想找到一个第三方Excel库,该库可以编写具有200,000多行的本机Excel文件而不杀死服务器,有什么想法吗?

Here's a really quick POC I made that writes 3 columns of 255 characters 200,000 times (600,000 cells). 这是我制作的一个非常快速的POC,可写3列255个字符,共200,000次(600,000个单元格)。 The final file comes in at 4.85MB on my machine. 最终文件在我的计算机上为4.85MB。

        string ExportFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.xlsx");
        string DSN = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES\";", ExportFile);

        using (System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection(DSN))
        {
            Con.Open();
            using (System.Data.OleDb.OleDbCommand Com = new System.Data.OleDb.OleDbCommand())
            {
                Com.Connection = Con;
                Com.CommandText = "CREATE TABLE [TestSheet] (A1 varChar(255), B1 varChar(255), C1 varChar(255))";
                Com.ExecuteNonQuery();
                string A1 = new string('A', 255);
                string B1 = new string('B', 255);
                string C1 = new string('C', 255);
                Com.CommandText = string.Format("INSERT INTO [TestSheet] (A1, B1, C1) VALUES ('{0}', '{1}', '{2}')", A1, B1, C1);
                for (var i = 1; i <= 200000; i++)
                {
                    Com.ExecuteNonQuery();
                }
            }
            Con.Close();
        }

On a server I'm not all sure what's needed but you might have to install this: 在服务器上,我不确定是否需要什么,但是您可能必须安装以下命令:

http://www.microsoft.com/downloads/en/details.aspx?familyid=7554f536-8c28-4598-9b72-ef94e038c891&displaylang=en http://www.microsoft.com/downloads/zh-CN/details.aspx?familyid=7554f536-8c28-4598-9b72-ef94e038c891&displaylang=en

Ultimately excel is a flat file system and SQL isn't making compatibility interesting. 最终excel是一个平面文件系统,而SQL并没有使兼容性变得有趣。 I'm hoping Chris Haas' code will work for you. 我希望Chris Haas的代码对您有用。 It does seem odd, 'I don't think csv is an option as the client want the numbers formatted correctly. 看起来确实很奇怪,“我不认为csv是一种选择,因为客户端希望数字格式正确。 ' if they have an SQL database they don't refer/query that instead of having an excel version of the database? '如果他们有SQL数据库,他们没有引用/查询而不是拥有数据库的excel版本?

我最终自己生成了一个xlsx,事实证明格式很简单

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

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