开发者

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. I don't think csv is an option as the client want the numbers formatted correctly. I've tried three third party Excel libraries but all have fallen over with so much data and are using gigabytes of memory.

I've wrote some code to generate an Excel XML file and it runs very quickly but the file is over 300megs. If I open and save as a native Excel file it gets it down to 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. 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?


Here's a really quick POC I made that writes 3 columns of 255 characters 200,000 times (600,000 cells). The final file comes in at 4.85MB on my machine.

        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


Ultimately excel is a flat file system and SQL isn't making compatibility interesting. I'm hoping Chris Haas' code will work for you. It does seem odd, 'I don't think csv is an option as the client want the numbers formatted correctly. ' if they have an SQL database they don't refer/query that instead of having an excel version of the database?


I ended up generating a xlsx myself, it turns out the format is pretty simple

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜