在导出的时候,在我的机器上导出excel没事,换了一台机器,就出现了乱码,这是怎么回事,代码如下:
if (this.DataGrid1.Items.Count==0) return;
string FileName="NewFile"+DateTime.Today.Year.ToString()+DateTime.Today.Month.ToString()+DateTime.Today.Day.ToString();
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition","attachment;filename="+FileName+".xls");
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid1.BackColor=System.Drawing.Color.White;
this.DataGrid1.BorderColor=System.Drawing.Color.Black;
this.DataGrid1.HeaderStyle.BackColor=System.Drawing.Color.White;
this.DataGrid1.HeaderStyle.ForeColor=System.Drawing.Color.Black;
this.DataGrid1.RenderControl(oHtmlTextWriter);
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
Response.Write(" 单位水、电、气收费明细表 ");
Response.Write(" 单位名称:"+this.dwmc.Text.ToString());
Response.Write(oStringWriter.ToString());
Response.End();