Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Export datatable to XLS/CSV compatible with MS-Office and Open-Office

0.00/5 (No votes)
29 Jun 2005 1  
It generates Excel using HTML tags, which are also compatible with Open-Office.

Sample Image - ExportXLSCSV.gif

Introduction

Most of the time we get a requirement from the client to export data into .XLS or .CSV format. These are the commonly used solutions by the developer:

  • Response.ContentType = ="application/vnd.ms-excel", this requires Excel to be properly installed on the client machine.
  • Now to save the cost, developer can generate the Excel file on the server, and later on download the generated file. But this still requires one license on the server and normally gives a performance problem as the Excel object is unmanaged.
  • Using XML/XSLT solution is available here, in which user can generate a file on server and download it without consuming Excel license on the server. Later, the user can modify the file if they have a full Excel version or view in case Excel viewer is installed.

But my client was slighly more conserned about the cost, they were using Open-Office and not the full version of Excel to save the license cost. Which leads to the development of this code library which makes XLS compatible with Open-Office and Excel without using any license.

Using this library code

This library is implemented in C# without using any unmanaged resource and it's very simple to use. Here are the examples:

  • To generate Excel:
    string fileName = 
      System.Guid.NewGuid().ToString().Replace("-","") + ".xls";
    (new ExportDemo.Libraries.ExcelConvertor()).Convert(
      GenerateDataTable(),Server.MapPath("."),fileName); 
    Response.Redirect(fileName);
  • To generate CSV:
    string fileName = 
      System.Guid.NewGuid().ToString().Replace("-","") + ".xls"; 
    (new ExportDemo.Libraries.ExcelConvertor()).Convert(
      GenerateDataTable(),Server.MapPath("."),fileName); 
    Response.Redirect(fileName);

Points of Interest

This component is as free as the .NET Framework library.

History

  • Initial version added on 29-June '05.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here