被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();
代码如下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();
解决方案 »
- **********100分问题异步委托抱错的问题******************
- c# databinder
- 想用winfrom+2008+c#写个程序,最小化驻留在任务栏上,用于每隔1小去运行一次updata.asp。小弟先感谢前辈了!
- 时间范围如何取?
- C# 得到oracle的实例名和数据库名
- 求助!需要一个c# directsound的例子
- 写了一个彩色的ASP.NET验证码组件
- 关于制作安装程序的问题
- 图片对比问题
- 在window Form中怎么取得DataGrid的指定单元的值?
- 关于C#调用API中数据类型的问题
- 求C#中使用DataWindow Designer 2.5的教程越全面越好
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)
{
Response.Charset = "GB2312"; //设置获取输出数据的类型
Response.ContentEncoding = System.Text.Encoding.UTF7;
//将Http头添加到输出流
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
Response.ContentType = FileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw); //将GridView中的内容输出到指定字符串中
Response.Write(tw.ToString());
Response.End(); //将当前面的所有缓冲发送到客户端
}
//如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
//将Excel中的数据导入到指定控件中
public DataSet CreateDataSource() {
string strCon = "provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("Excel.xls")+"; Extended Properties= Excle 8.0";
OleDbConnection olecon=new OleDbConnection(strCon);
OleDbDataAdapter myda=new OleDbDataAdapter("select * from [Sheet1$]",strCon);
DataSet myds=new DataSet();
myda.Fill(myds);
return myds;
}
//将数据从Excel导入到指定控件中
protected void btnExcelInfo_Click(object sender, EventArgs e)
{
GridView1.DataSource= CreateDataSource();
GridView1.DataBind();
}
}
2附带我最近遇到的一个小例子,打印出HTML页面内容
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMdd") + ".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = true;
后来在一个网页中找到了解决方法:
Response.Write("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=gb2312\">");
Response.Write(sb.ToString());
Response.Write("</body></html>");
这样就不会有问题了。
虽然不是在这里找到的答案,但是很感谢大家对我的帮助,给分结贴!!!