Export data as Excel file from ASP.NET

I have data like below


Now there is a button on the page, and when I click the button, the browser would download an Excel file with the data above, and stay on the current page. Is there any simple way to do it? The data is very simple. Only one column, and not huge.

Best Regards,


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can write out the data directly to the response stream. Set the mime type to excel and write the data out as :

  • HTML
  • CSV
  • Spreadsheet XML
  • OOXML (.xlsx)

If you want to use OOXML there are libraries such as Simple OOXML. Note this is the .xlsx format.

The following code sets the headers required for a .xls file

'Send response with content type to display as MS Excel
context.Response.Buffer = True

context.Response.AddHeader("content-disposition", String.Format( "attachment;filename={0}", fileName))
context.Response.ContentEncoding = Encoding.UTF8

context.Response.ContentType = "application/vnd.ms-excel"

'Write to response


Method 2

Yes, you can export the information as a csv file, and give it an Excel file extension. Excel will recognize it’s not native Excel format and allows to import with a warning message. For the download, you can search the internet for regular file downloads with a custom MIME type in ASP.NET.

Because of the warning message, the above is not the preferred way. You could also use a library to generate a native Excel file. I’ve used Aspose.Cells successfully in past projects.

Method 3

Short of automating excel on your server, which is not recommended, the easiest way is to use a string builder to create the required output and then write this to the HttpResponse setting the content type to “text/csv”, setting the appropriate header information.

While this is not strictly an excel document the user downloading the file will be prompted to open it in excel if they have it installed or alternatively any other spreadsheet based editor.

The following snippet should get you going:

string docName  = "MyExcelDoc"
StringBuilder sb = new StringBuilder();

context.Response.ContentType = "text/csv";
context.Response.AddHeader("content-disposition", "attachment; filename=" + docName + ".csv");

Method 4

An alternative to the above mentioned solutions is to hook into the Excel COM object and generate a spreadsheet using their API.

This would mean you would have to have excel installed on the server so the other solutions are probably better.

Method 5

NPOI is an open source project which can help you read/write xls, doc, ppt files.

Very simple and give right Excel format.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x