C#导出至excel中文乱码的问题
被C#导出到excel中文乱码的问题折磨了好多天了,尝试过网上说的各种方法,都解决不了,关键是乱码时有时没有,真的很崩溃,菜鸟求救,各位高手帮帮忙啊,先谢了~
代码如下
string filename = "aaa";
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8).ToString());
Response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Write(sb.ToString());
Response.Flush();
Response.End();
------最佳解决方案--------------------试试
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
------其他解决方案--------------------pre class="csharp" name="code">using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
Bind(); //窗体加载时向GridView中填充数据
}
}
PosDataContext pos = new PosDataContext(); //使用Linq to sql进行操作数据库的数据
//自定义向GridView控件填充数据方法
private void Bind()
{
GridView1.DataSource = pos.Product.ToList(); //填充数据
GridView1.DataKeyNames = new string[] {"Id" }; //设置主键字段
GridView1.DataBind(); //将数据绑定到控件中
}
//导出数据事件
protected void btnExcel_Click(object sender, EventArgs e)
{
Export("application/ms-excel","商品信息报表.xls");
}
//自定义导出数据的方法
private void Export(string FileType, string FileName)
{
Respons