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 ?

Popularity: 100% [?]

This post was written by:

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

Software Engineer, Sri Lanka.

Contact the author

8 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.

  3. Karthikeyan Says:

    Hi,

    For Formatting excel output, this ExcelXMLWriter is perfectly working fine.. This is taking inputs only either from DataSet or DataTable only. Not from other media. Now I want to generate a graph just below the formatted output.How to export IMAGE also into the excel sheet. Please guide me in this regard,
    Karthik

  4. Ghufran Says:

    Hi pongscript, Actually it exports in Ms 2007 format. If you change the file format “xls” to “xlsx”, it will work fine.
    How to export in XLS format?

    Anybody…

  5. ALZDBA Says:

    I have used this to generate xls files. Later on in this vb.net2008 these xls files are being emailed (smtp) and the sent files should be moved to an archive folder.

    This move operation does not work !!
    Apparently the files are still locked, even though the (workbooks) excellapp has been saved, closed and quit.

    Should I release these file locks using another methode ?

    Johan

  6. Anz4rt Says:

    Really helpfull. I hope this is work greatefully for my work.

  7. Dinesh Says:

    Good Stuff! Thanks Aneef. Hope this “CarlosAg.ExcelXmlWriter” component is trustable.

  8. freeman_vt Says:

    Thanks, this is a great tool! But i need to export to Exel binary workbook files, can it do this? please show me how.

Leave a Reply

Follow me