RSS

Create Excel Sheets Programmatically with C# without Installing Excel.

Fri, Jul 25, 2008

.Net Related, ADO.net, Libraries, c#

Here goes one more needy component for developers. a lot of developers always had a problem of exporting data to excel programmatically. if you Google this topic you will get many results regarding this. here are some which caught my eyes.

  1. How To Use ADO.NET to Retrieve and Modify Records in an Excel Workbook With Visual Basic .NET
  2. DataSet to Excel in Two steps – with Different Styles- by Krishna_accent
  3. And finally comes our good old CSV files.

the first one seems to be ok. but have little more work to be done on coding. the second is a  really good one, but again we need excel to be installed, which again run out of our needs.

using CSV is ok for unformatted Comma Separated Values.

 

But Recently I found Carlos Aguilar Mares has this wonderful FREE component called ExcelXmlWriter , to export data to excel. it doesn’t need excel to be installed and it can be used to generate formatted excel workbooks with very few lines of code.

 

   1:  using CarlosAg.ExcelXmlWriter;
   2:   
   3:  class TestApp {
   4:      static void Main(string[] args) {
   5:          Workbook book = new Workbook();
   6:          Worksheet sheet = book.Worksheets.Add(“Sample”);
   7:          WorksheetRow row =  sheet.Table.Rows.Add();
   8:          row.Cells.Add(“Hello World”);
   9:          book.Save(@”c:\test.xls”);
  10:      }
  11:  }

the download has a very easy to understand help file explaining how to use this. you can download it in his website here.

Clean and simple isn’t it ?

This post was written by:

Aneef Fashir - who has written 19 posts on Aneef.Net.

Software Engineer, Sri Lanka.

Contact the author

2 Comments For This Post

  1. Steve Carnes Says:

    This was wonderful help! Thanks! I was looking for a way to export a DataGridView’s data to Excel without needing to use the com objects, as that can be very messy. This was a lifesaver.

  2. pongscript Says:

    try opening that in open office. and you’ll see xml structured file. I see this as a vunerability. since you can read it as plain text. I wish there’s a way to convert it to binary format. since this is a great tool.

Leave a Reply