Asp.net导出Excel乱码
类别:编程学习 浏览量:248
时间:2014-5-29 Asp.net导出Excel乱码
Asp.net导出Excel乱码一、设置编码格式为"UTF-8"
解决方法关键代码
Response.Charset = "UTF-8";//添加编码格式
Response.ClearContent();
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;//表格内容添加编码格式
Response.HeaderEncoding = System.Text.Encoding.UTF8;//表头添加编码格式
例如
protected void Excel_Click(object sender, EventArgs e)
{
Response.Charset = "UTF-8";
Response.ClearContent();
Response.Clear();
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.HeaderEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("content-disposition", "attachment; filename=MyExpress。xls");
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// turn off paging
GridView1.AllowPaging = false;
dataBind();
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
// turn the paging on again
GridView1.AllowPaging = true;
dataBind();
}
二、"UTF8"格式输出还是乱码,则用 "gb2312"
关键代码为
Response.ClearContent();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=sumlate。xls");
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/excel";
System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
if (GridView2.Rows.Count > 0)
{
GridView2.RenderControl(htw);
}
else
{
GridView1.RenderControl(htw);
}
Response.Write(sw.ToString());
Response.End();
附加实例
/// <summary>
/// 把DataTable内容导出excel并返回客户端
/// </summary>
/// <param name="dgData">待导出的DataTable</param>
public static void DataTable2Excel(System.Data.DataTable dtData)
{
System.Web.UI.WebControls.DataGrid dgExport = null;
// 当前对话
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
// IO用于导出并返回excel文件
System.IO.StringWriter strWriter = null;
System.Web.UI.HtmlTextWriter htmlWriter = null;
if (dtData != null)
{
// 设置编码和附件格式
curContext.Response.ContentType = "application/vnd.ms-excel";
curContext.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
curContext.Response.Charset = "gb2312";
// 导出excel文件
strWriter = new System.IO.StringWriter();
htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
// 为了解决dgData中可能进行了分页的情况,需要重新定义一个无分页的DataGrid
dgExport = new System.Web.UI.WebControls.DataGrid();
dgExport.DataSource = dtData.DefaultView;
dgExport.AllowPaging = false;
dgExport.DataBind();
// 返回客户端
dgExport.RenderControl(htmlWriter);
curContext.Response.Clear();
curContext.Response.Write("<meta http-equiv=\\"content-type\\" content=\\"application/ms-excel; charset=gb2312\\"/>" + strWriter.ToString());
curContext.Response.End();
}
}
标签:Excel乱码
热门推荐
- sql导入数据库脚本(使用用SQL语句从电脑导入图片到数据库的方法)
- vue功能测试和生产环境切换(vue 单元测试的推荐插件和使用示例)
- dedecms使用教程(织梦dedecms软件频道判断是本站下载链接后再列出镜像的方法)
- php 经典模式(php设计模式之装饰模式应用案例详解)
- laravel零基础(基于laravel Request的所有方法详解)
- css3渐变背景图片(css3实现背景图片半透明内容不透明的方法示例)
- js对日期加减指定天、时、分、秒
- phpstudy报错(phpStudy运行帝国备份王出错解决方法)
- sql server 视图操作(Sql Server 视图数据的增删改查教程)
- mysql中如何进行模糊查询(MySQL模糊查询用法大全正则、通配符、内置函数)