Yongwei Xing
Yongwei Xing

Reputation: 13441

Export data as Excel file from ASP.NET

I have data like below

AAAAAA
BBBBBB
CCCCCC
DDDDDD
EEEEEE

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,

Upvotes: 4

Views: 14102

Answers (5)

James Westgate
James Westgate

Reputation: 11444

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.Clear()
context.Response.Buffer = True

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

context.Response.Cache.SetCacheability(HttpCacheability.Private)
context.Response.ContentType = "application/vnd.ms-excel"

'Write to response
context.Response.Write("csv,data,goes,here")

context.Response.End()

Upvotes: 7

Abdul Munim
Abdul Munim

Reputation: 19217

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

Very simple and give right Excel format.

Upvotes: 0

Burt
Burt

Reputation: 7758

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.

Upvotes: 0

R Bremner
R Bremner

Reputation: 216

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();
sb.AppendLine("AAAAAA");
sb.AppendLine("BBBBBB");

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

Upvotes: 0

Bart Verkoeijen
Bart Verkoeijen

Reputation: 17753

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.

Upvotes: 0

Related Questions