Click here to Skip to main content
15,879,474 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
2.41/5 (22 votes)
28 Jun 20051 min read 141.9K   3.7K   48   13
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:
    C#
    string fileName = 
      System.Guid.NewGuid().ToString().Replace("-","") + ".xls";
    (new ExportDemo.Libraries.ExcelConvertor()).Convert(
      GenerateDataTable(),Server.MapPath("."),fileName); 
    Response.Redirect(fileName);
  • To generate CSV:
    C#
    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


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionNice Article Pin
swaroop10695-Jun-12 2:10
swaroop10695-Jun-12 2:10 
GeneralMy vote of 4 Pin
Morteza Karimian Kelishadrokhi4-Oct-11 19:30
Morteza Karimian Kelishadrokhi4-Oct-11 19:30 
GeneralMy vote of 5 Pin
delko27-Sep-10 12:39
delko27-Sep-10 12:39 
GeneralVB Version Pin
frank stuart browne9-Jul-09 17:08
frank stuart browne9-Jul-09 17:08 
GeneralRe: VB Version Pin
dhanashivam26-Jan-10 19:13
dhanashivam26-Jan-10 19:13 
GeneralUseful Pin
padmanabhan N19-Nov-08 0:19
padmanabhan N19-Nov-08 0:19 
Generala bug Pin
shaorang11-Apr-08 19:44
shaorang11-Apr-08 19:44 
GeneralGreat! Pin
sraap19728-Mar-07 9:47
sraap19728-Mar-07 9:47 
GeneralHi Pin
ashvinidave5-Jan-07 9:54
ashvinidave5-Jan-07 9:54 
GeneralRe: Hi Pin
Vivek Singh - IN8-Jan-07 0:39
Vivek Singh - IN8-Jan-07 0:39 
GeneralRe: Hi Pin
CikaPero19-Sep-11 21:55
CikaPero19-Sep-11 21:55 
GeneralExport Russian Character Data to CSV or XLS File Pin
MSJ_PK3-Aug-05 0:03
MSJ_PK3-Aug-05 0:03 
GeneralRe: Export Russian Character Data to CSV or XLS File Pin
Vivek Singh - IN3-Aug-05 0:58
Vivek Singh - IN3-Aug-05 0:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.