【发布时间】:2015-02-13 10:13:46
【问题描述】:
我目前正在开发一个简单的程序(使用 ASP.Net C#)将数据从 GridView 填充到 Excel 文件中。需要将 Excel 文件下载到客户端计算机中。
由于某些原因,我需要在 Excel 文件下载到客户端本地计算机后快速对其进行操作。
问题是我无法获取下载文件的位置。 下载到客户端计算机后如何获取文件位置?
这是截图:
下载代码:
private void Create_ExcelContent2()
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + ddlBatchID.Text + ".xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw); ...
gvBatchSummary.RenderControl(hw);
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
【问题讨论】:
-
显示您用于下载文件的代码
-
私人无效 Create_ExcelContent2() { Response.Clear();响应缓冲区=真; Response.AddHeader("content-disposition", "attachment;filename=" + ddlBatchID.Text + ".xls"); Response.Charset = ""; Response.ContentType = "应用程序/vnd.ms-excel";使用 (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); ... gvBatchSummary.RenderControl(hw);字符串样式 = @""; Response.Write(风格); Response.Output.Write(sw.ToString()); Response.Flush();响应。结束(); } }
-
在“Respone.Flush()”行执行后显示保存对话框。
-
编辑您的问题(并格式化),不要在 cmets 中添加代码。
-
@fubo 我只需要地址。获得地址后,我将使用它来获取文件(因为我已经有了地址),打开文件并将其内容复制到一个新的空 Excel 文件中。它。