关于asp.net导出excel的注意(6)
protected void ExportToExcel(System.Web.UI.WebControls.DataGrid grid, string file)
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename=" + file); //定义输出文件和文件名
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。
EnableViewState = false;
{
Response.Clear();
Response.Buffer= true;
Response.Charset="GB2312"; //设置了类型为中文防止乱码的出现
Response.AppendHeader("Content-Disposition","attachment;filename=" + file); //定义输出文件和文件名
Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
Response.ContentType = "application/ms-excel"; //设置输出文件类型为excel文件。
EnableViewState = false;
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(ci);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
grid.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
如果遇到[类型“DataGridLinkButton”的控件“DataGrid1__ctl1__ctl0”必须放在具有 runat=server 的窗体标记内。] 这样的错误的话。
解决办法:
导出之前将出错列显示属性设置为false就行了。
this.grd.Columns[出错列的index].Visible=false;
浙公网安备 33010602011771号