当前位置: 代码迷 >> ASP.NET >> 为什么导出来的Excel内容是乱码?看下小弟我的代码吧
  详细解决方案

为什么导出来的Excel内容是乱码?看下小弟我的代码吧

热度:2698   发布时间:2013-02-25 00:00:00.0
为什么导出来的Excel内容是乱码?看下我的代码吧.
private   void   ReBut5_Click(object   sender,   System.EventArgs   e)
{
if(this.ReGrid1.Items.Count> 0)
{
this.ButExcel( "application/ms-excel ", "加班時間大於36小時.xls ");
}
else
{
Page.RegisterStartupScript( " ", " <script> alert( '沒有資料導出! ') </scrip> ");
}
}
public   void   ButExcel(string   FileType,   string   FileName)
{

System.IO.StringWriter   tw=   new   System.IO.StringWriter();
System.Web.UI.HtmlTextWriter   hw   =   new   System.Web.UI.HtmlTextWriter(tw);
Response.Charset   =   "Big5 ";
Response.ContentEncoding   =   System.Text.Encoding.UTF8;
Response.AppendHeader( "Content-Disposition ",   "attachment;filename= "   +   HttpUtility.UrlEncode(FileName,   System.Text.Encoding.UTF8).ToString());
Response.ContentType   =   FileType;
this.EnableViewState   =   false;
Response.Clear();
Response.Buffer=   true;
DataGrid   dg=new   DataGrid();
SqlConnection   Con   =   new   SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "Con "]);
Con.Open();
SqlDataAdapter   sda   =   new   SqlDataAdapter();
sda.SelectCommand   =   new   SqlCommand( "select   WorkId   as   '工號 ',[name]   as   '姓名 ',FactTotal   as   '實際加班時數 ',dept   as   '部門 '   from   V_ReportJB   where   facttotal   >   36 ",Con);
DataSet   ds   =   new   DataSet();
sda.Fill(ds, "table ");
dg.DataSource=ds;
dg.DataBind();
dg.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

}


------解决方案--------------------------------------------------------
这句
Response.Charset = "Big5 ";
改为
Response.Charset = "GBK ";
  相关解决方案