简体   繁体   English

从ASP.NET导出数据为Excel文件

[英]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. 现在页面上有一个按钮,当我单击按钮时,浏览器将下载包含上述数据的Excel文件,并保留在当前页面上。 Is there any simple way to do it? 有没有简单的方法呢? The data is very simple. 数据非常简单。 Only one column, and not huge. 只有一列,而不是巨大的。

Best Regards, 最好的祝福,

You can write out the data directly to the response stream. 您可以将数据直接写入响应流。 Set the mime type to excel and write the data out as : 将mime类型设置为excel并将数据写为:

  • HTML HTML
  • CSV CSV
  • Spreadsheet XML 电子表格XML
  • OOXML (.xlsx) OOXML(.xlsx)

If you want to use OOXML there are libraries such as Simple OOXML . 如果您想使用OOXML,可以使用Simple OOXML等库。 Note this is the .xlsx format. 请注意,这是.xlsx格式。

The following code sets the headers required for a .xls file 以下代码设置.xls文件所需的标头

'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()

Yes, you can export the information as a csv file, and give it an Excel file extension. 是的,您可以将信息导出为csv文件,并为其提供Excel文件扩展名。 Excel will recognize it's not native Excel format and allows to import with a warning message. Excel将识别它不是本机Excel格式,并允许导入带有警告消息。 For the download, you can search the internet for regular file downloads with a custom MIME type in ASP.NET. 对于下载,您可以在Internet上搜索使用ASP.NET中的自定义MIME类型的常规文件下载。

Because of the warning message, the above is not the preferred way. 由于警告信息,以上不是首选方式。 You could also use a library to generate a native Excel file. 您还可以使用库来生成本机Excel文件。 I've used Aspose.Cells successfully in past projects. 我在过去的项目中成功使用了Aspose.Cells。

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. 如果不推荐在服务器上自动执行excel,最简单的方法是使用字符串构建器创建所需的输出,然后将其写入HttpResponse,将内容类型设置为“text / csv”,设置相应的头信息。

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. 虽然这不是严格意义上的excel文档,但如果安装了文件,则提示用户下载文件将在excel中打开它,或者任何其他基于电子表格的编辑器。

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();

An alternative to the above mentioned solutions is to hook into the Excel COM object and generate a spreadsheet using their API. 上述解决方案的替代方法是挂钩Excel COM对象并使用其API生成电子表格。

This would mean you would have to have excel installed on the server so the other solutions are probably better. 这意味着您必须在服务器上安装excel,以便其他解决方案可能更好。

NPOI is an open source project which can help you read/write xls, doc, ppt files. NPOI是一个开源项目,可以帮助您读/写xls,doc,ppt文件。

Very simple and give right Excel format. 非常简单,并给出正确的Excel格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM