我正在将gridview导出为excel,我正在成功导出。但在我的gridview中我有一些特殊的符号,如(•,“”,“, - ,_)这些类型的符号无法导出,而不是那些地方我是变得像(“,”,“,”)。 我怎么能过来这些问题呢。这是我将gridview导出为excel的代码吗?
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=demo.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
gvDetails.AllowPaging = false;
gvDetails.DataSource = dt1;
gvDetails.DataBind();
for (int i = 0; i < gvDetails.Rows.Count; i++)
{
GridViewRow row = gvDetails.Rows[i];
row.Attributes.Add("class", "textmode");
}
gvDetails.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { mso-number-format:\@; } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
答案 0 :(得分:1)
您需要指定并提供编码信息。
Response.ContentEncoding = System.Text.Encoding.Unicode; /*c# uses UTF-16 internally*/
...
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); /*Include the UTF-16 header so excel will treat this as UTF-16*/
Response.Output.Write(sw.ToString());
...
请注意,这不是最佳解决方案,理想情况下,您应该使用OpenXML或其他库导出到Excel。
答案 1 :(得分:0)
请将您的内容类型更改为以下
Response.ContentType = "application/ms-excel";