[英]Export Datagrid data to Excel C#
我已經將字符串Array綁定到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標記,請問我的代碼有什么問題嗎? 請幫忙!!
您需要使用TransmitFile
或BinaryWrite
方法將文件編寫為二進制文件,僅使用HtmlTextWriter
將無濟於事。
看這里 。
這可能有幫助!
Response.Buffer = true;
Response.ContentType = "application/text";
Response.AppendHeader("Content-Disposition", "attachment; filename=file1.xls");
Response.TransmitFile(fileName);
Response.Flush();
Response.End();
使用Response.Write而不是Response.Output.Write。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.