我已经将字符串数组绑定到Datagrid,然后我需要通过自动将文件保存在客户机中将数据导出到excel文件。 以下是我使用的代码。导出Datagrid数据到Excel C#
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标签,可我知道自己做错了什么,以我的代码?请帮忙!!
尝试导出csv格式,而不是xlsx格式.. http://stackoverflow.com/questions/13563343/simple-way-to-export-datagridview-to-excel –
tip - http://social.msdn。 microsoft.com/Forums/vstudio/en-US/08d69589-4052-410f-a85c-c38493c0a664/creatingwriting-to-an-excel-file-with-c –