Export data as Excel file from ASP.NET

后端 未结 5 1078
半阙折子戏
半阙折子戏 2020-12-17 05:27

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

相关标签:
5条回答
  • 2020-12-17 05:33

    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()
    
    0 讨论(0)
  • 2020-12-17 05:46

    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.

    0 讨论(0)
  • 2020-12-17 05:48

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

    Very simple and give right Excel format.

    0 讨论(0)
  • 2020-12-17 05:54

    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.

    0 讨论(0)
  • 2020-12-17 05:57

    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();
    
    0 讨论(0)
提交回复
热议问题