从ASP.NET将数据导出为Excel文件 [英] Export data as Excel file from ASP.NET

查看:98
本文介绍了从ASP.NET将数据导出为Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据

AAAAAA
BBBBBB
CCCCCC
DDDDDD
EEEEEE

现在页面上有一个按钮,当我点击按钮时,浏览器将下载具有上述数据的Excel文件,并保留在当前页面上。有什么简单的方法吗?数据很简单。

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.

最好的问候,

推荐答案

您可以将数据直接写入响应流。将mime类型设置为excel并将数据写入:

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

  • 电子表格XML

  • OOXML(.xlsx)

如果您想使用OOXML,则会有一些库,例如简单OOXML 。注意这是.xlsx格式。

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

以下代码设置.xls文件所需的标题

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

这篇关于从ASP.NET将数据导出为Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆