【发布时间】:2023-03-07 10:29:01
【问题描述】:
通过 SqlDataSource 绑定 GridView,我写了下面的代码从 GridView 导出到 Excel:
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
gvReportPrint.GridLines = GridLines.Both;
gvReportPrint.Font.Name = "'BYekan'";
foreach (GridViewRow row in gvReportPrint.Rows)
{
row.Cells[2].Attributes.Add("class", "textmode");
}
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
gvReportPrint.HeaderStyle.Font.Bold = true;
Response.Write(style);
gvReportPrint.RenderControl(hw);
Response.Output.Write(sw.ToString());
Response.End();
在从 GridView 导出到 Excel 期间,Unicode 字符无法正确显示, 它们显示如下: --> Click this link to show the problem <--
【问题讨论】:
标签: excel sql-server-2008 utf-8 export unicode-string