asp.net将数据导出到excel或word时为什么会是乱码
需导入using System.Text;
在以下按钮单击事件中实现:
private void Button1_Click(object sender, System.EventArgs e)
{
Response.ContentType = "application/vnd.ms-excel ";
Response.AddHeader( "Content-Disposition ", "inline;filename= "
+ HttpUtility.UrlEncode( "下载文件.xls ",Encoding.UTF8 ) );
//如果输出为Word,修改为以下代码
//Response.ContentType = "application/ms-word "
//Response.AddHeader( "Content-Disposition ", "inline;filename=test.doc ")
StringBuilder sb=new StringBuilder();
System.IO.StringWriter sw = new System.IO.StringWriter(sb);
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
sb.Append( " <html> <body> ");
DataGrid1.RenderControl(hw);
sb.Append( " </body> </html> ");
Response.Write(sb.ToString());
Response.End();
}
请大家帮帮忙!!谢谢大家了
------解决方案--------------------Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312 ";
Response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312 ");
string FileNames = DateTime.Now.ToString( "yyyyMMddHHmmss ") + ".xls ";
Response.AddHeader( "Content-Disposition ", "attachment;filename= " + FileNames);
Response.ContentType = "application/ms-excel ";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
这个是我的导出excel
------解决方案--------------------Response.Charset = "GB2312 ";
Response.ContentEncoding = System.Text.Encoding.GetEncoding( "GB2312 ");
没错
再BS下楼主
搞定了也应该给分啊~!
最少证明下人家的劳动成果吧~!