SIGN UP MEMBER LOGIN:    
ARTICLE

Fastest way to read and write to Excel

Posted by Amit Choudhary Articles | Office Development March 14, 2011
Here we’ll discuss efficient ways to write large volumes of data to Excel with the help of some easy tricks.
Reader Level:


Fastest way to write to Excel

Many times I have to work on generating an Excel sheet and there was a time when the user reported that the generation of Excel reports was quite slow. While researching that I tried some Google searches and discovered that there's a way to write a large volune of data to excel very quickly. After I did the R&D on the code I thought to share it.

Here we'll not discuss how to create an Excel sheet programmatically but we'll see how we can write to Excel a large amount of data in the quickest manner possible. Use the trick mentioned below and impress your management.

Here's goes the code to create an Excel workbook and worksheet:

Microsoft.Office.Interop.Excel.Application xlApp = null;
   Workbook xlWorkbook = null;
   Sheets xlSheets = null;
   Worksheet xlNewSheet = null;
xlApp = new Microsoft.Office.Interop.Excel.Application();
xlWorkbook = xlApp.Workbooks.Open(sourcefile, Type.Missing, false, Type.Missing, Type.Missing, Type.Missing,
               false, XlPlatform.xlWindows, Type.Missing,
               true, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
   // xlWorkbook = xlApp.Workbooks.Add(Type.Missing);
         xlSheets = xlWorkbook.Sheets as Sheets;
 
   // The first argument below inserts the new worksheet as the first one
         xlNewSheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
xlNewSheet.Name= "
DummySheet";

Now here's the discussion of what to do after you have accomplished the preceding to create a sheet.

Most of the time we write a loop and then write the data cell by cell. And this makes the application go slower.

If you have your data in a collection then your best method is to write the data using a range.

var startCell = (Range)xlNewSheet.Cells[2, 1];
      var endCell = new object();
      endCell = (Range)xlNewSheet.Cells[numbers.Items.Count, 6];
      var writeRange = xlNewSheet.get_Range(startCell, endCell);
      writeRange.set_Value(Type.Missing, retList);

and after that release everything and quit so you don't loose your data.

xlWorkbook.Save();
      xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);
      xlApp.Quit();

and finally release your COM objects.

finally
  { 
      if(xlNewSheet!=null)
      Marshal.ReleaseComObject(xlNewSheet);
      if (xlSheets != null)
      Marshal.ReleaseComObject(xlSheets);
      if (xlWorkbook != null)
      Marshal.ReleaseComObject(xlWorkbook);
      if (xlApp != null)
      Marshal.ReleaseComObject(xlApp);
      xlApp = null;
      KillExcel();
  }

Happy Reporting :)).

Login to add your contents and source code to this article
share this article :
post comment
 

Mahesh, wat I spoked about was "production servers" and not individual dev machines/ dev servers. Even the project which I worked was about to be hosted in the prod. server present in US. Recent project of mine had these problems(not having Office suite installed on prod. servers) and I was forced to comment the code that used Interop. I found an alternative way to create the spreadsheet using a ExcelWriter class. Please go thru the below link: http://www.codeproject.com/KB/office/biffcsharp.aspx Even the traditional method of generating excel (Response.ContentType) works fine fpr just to generate the file, but if it happens for you to upload that excel again and read it fails. Regards, Sayed Amin

Posted by Amin Sayed Mar 17, 2011

I would not say "Not useful". Most of the developers and users in USA do have Office installed on their machines. I believe Amit wanted to show how to do this in code if you have Office on your machine. It is not intended for every user. Cheers!

Posted by Mahesh Chand Mar 16, 2011

Not at all useful, if the Microsoft Office is not installed on the Server which happens in 99.99% cases.

Posted by Amin Sayed Mar 16, 2011

I think it belonged to the Office Interop category.

Posted by Mahesh Chand Mar 14, 2011

Yes good catch suthish.. but actually first i was writing for the both but i was more focused more on "Write Excel" part and just skipped the "Read" part. Well, the part i missed was basically to take the advantage or OleDbDataReader over the OleDbDataAdapter. Since the Reader objects are faster then Adapter(use to fill the datatable/dataset) if you are using the OleDb to read the Excel. Thanks.

Posted by Amit Choudhary Mar 14, 2011
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Nevron Gauge for SharePoint
Become a Sponsor