导出excel的奇怪问题
从网上考了一段datagrid导出excel的代码,自己修改了一下,可是遇到个奇怪的问题,在datagrid里的数据少于10行时,导出的excel文件会是乱码,大于10行一切正常! 希望有达人可以指点一下!
private void Button1_Click(object sender, System.EventArgs e)
{
try
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
string strFileTitle = HttpUtility.UrlEncode(Request.QueryString["reportName"]);
Response.AppendHeader("Content-Disposition","attachment;filename="+strFileTitle+".xls");
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
//define a io
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid2.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
catch(Exception er)
{
string aa= er.ToString();
aa +="";
}
}
{
try
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312";
string strFileTitle = HttpUtility.UrlEncode(Request.QueryString["reportName"]);
Response.AppendHeader("Content-Disposition","attachment;filename="+strFileTitle+".xls");
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
//define a io
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.DataGrid2.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
catch(Exception er)
{
string aa= er.ToString();
aa +="";
}
}