我有一个ASP.NET网页,其中包含用户数据的网格视图.我需要获取gridview中的数据,生成excel文件,并允许用户将该excel文件保存到本地计算机.
在代码中完成此操作的最佳方法是什么?是否可以显示文件保存对话框,然后在用户选择目录和文件名后从网格数据创建文件?我已经在软件开发方面做到了这一点,但从未在Web开发中处理过.
谢谢
大多数人倾向于像这样实现它:
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
Run Code Online (Sandbox Code Playgroud)
另一种选择(我更喜欢的是)使用EPPLus
| 归档时间: |
|
| 查看次数: |
1666 次 |
| 最近记录: |