tags:

views:

230

answers:

5

Hi all

I have data like below

AAAAAA
BBBBBB
CCCCCC
DDDDDD
EEEEEE

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

Best Regards,

A: 

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.

bgever
A: 

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();
R Bremner
Use this methond, the HTML sorece code also in the csv file.
Yongwei Xing
Did you make sure to call "ClearContent" first ?
R Bremner
+2  A: 

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()
James Westgate
Sorry about using vb.net, its what I had lying around - the formatting is not doing too well
James Westgate
A: 

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.

Burt
A: 

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

Very simple and give right Excel format.

Munim Abdul