我已将字符串数组绑定到 Datagrid,然后我需要通过在客户端机器中自动保存文件来将数据导出到 excel 文件。下面是我使用的代码。
string fileName = "attachment;filename= DetailReport.xlsx";
Response.Clear();
Response.AddHeader("content-disposition", fileName);
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdExcel.RenderControl(htmlWrite);
Response.Output.Write(stringWrite.ToString());
Response.Flush();
Response.End();
我成功导出文件并保存在客户端计算机中,但文件中的内容包括所有 HTML 标签,我可以知道我的代码有什么问题吗?请帮忙!!