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

查看:85
本文介绍了出口数据从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类型创先争优和数据写出如下:

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

  • 取值​​preadsheet 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.

以下code设定一个.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天全站免登陆