Datagrid 导入到EXcel乱码的朋友,这里绝对解决
来源:互联网 发布:nba球员详细体测数据 编辑:程序博客网 时间:2024/06/07 08:55
今天怎么有客户反应导出的excel的乱码,??!!我记得我测试过的啊,后来我用服务器上的再测了下,确实有的会乱码有的却不会,真是奇怪,后来改它的编码utf-8 big5 都试了,就是不行,网上找了一个遇到同样问题的朋友的解决方案,确实可行!!不错,谢谢
被注释的是我之前用的,不好的方法,
private void BtnPushExcel_Click(object sender, System.EventArgs e)
...{
string fileName = "Excel.xls";
Export(this.DataGrid1,fileName);
// HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
// HttpContext.Current.Response.Charset ="UTF-8";
// //HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("BIG5");
// HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("BIG5");
// HttpContext.Current.Response.ContentType ="application/ms-excel";
// Datagrid2.Page.EnableViewState =false;
// System.IO.StringWriter tw = new System.IO.StringWriter() ;
// System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
// this.DataGrid1.RenderControl(hw);
// HttpContext.Current.Response.Write(tw.ToString());
// HttpContext.Current.Response.End();
}
...{
string fileName = "Excel.xls";
Export(this.DataGrid1,fileName);
// HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
// HttpContext.Current.Response.Charset ="UTF-8";
// //HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("BIG5");
// HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("BIG5");
// HttpContext.Current.Response.ContentType ="application/ms-excel";
// Datagrid2.Page.EnableViewState =false;
// System.IO.StringWriter tw = new System.IO.StringWriter() ;
// System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
// this.DataGrid1.RenderControl(hw);
// HttpContext.Current.Response.Write(tw.ToString());
// HttpContext.Current.Response.End();
}
private void Export(System.Web.UI.WebControls.DataGrid dg,string fileName)
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("BIG5");
httpResponse.ContentType ="application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dg.RenderControl(hw);
string filePath = Server.MapPath("..")+fileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();
DownFile(httpResponse,fileName,filePath);
httpResponse.End();
}
private bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
{
try
{
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=BIG5");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=102400;//每100K同时下载数据
byte[] readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}
{
System.Web.HttpResponse httpResponse = Page.Response;
httpResponse.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8));
httpResponse.ContentEncoding=System.Text.Encoding.GetEncoding("BIG5");
httpResponse.ContentType ="application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter (tw);
dg.RenderControl(hw);
string filePath = Server.MapPath("..")+fileName;
System.IO.StreamWriter sw = System.IO.File.CreateText(filePath);
sw.Write(tw.ToString());
sw.Close();
DownFile(httpResponse,fileName,filePath);
httpResponse.End();
}
private bool DownFile(System.Web.HttpResponse Response,string fileName,string fullPath)
{
try
{
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename=" +
HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + ";charset=BIG5");
System.IO.FileStream fs= System.IO.File.OpenRead(fullPath);
long fLen=fs.Length;
int size=102400;//每100K同时下载数据
byte[] readData = new byte[size];//指定缓冲区的大小
if(size>fLen)size=Convert.ToInt32(fLen);
long fPos=0;
bool isEnd=false;
while (!isEnd)
{
if((fPos+size)>fLen)
{
size=Convert.ToInt32(fLen-fPos);
readData = new byte[size];
isEnd=true;
}
fs.Read(readData, 0, size);//读入一个压缩块
Response.BinaryWrite(readData);
fPos+=size;
}
fs.Close();
System.IO.File.Delete(fullPath);
return true;
}
catch
{
return false;
}
}
- Datagrid 导入到EXcel乱码的朋友,这里绝对解决
- Datagrid 导入到EXcel乱码的朋友,这里绝对解决
- excel数据导入到datagrid
- excel数据导入到datagrid
- 关于DataGrid 数据导入Excel乱码
- DataGrid的数据导入到Excel或CSV
- 将datagrid数据导入到excel的方法
- 将datagrid中显示的内容导入到Excel中
- easyui datagrid 导出excel及乱码解决
- 将datagrid数据导入到excel(转载)
- 将EXCEL 数据导入到 DataGrid中
- 解决Excel导入mysql数据库时汉字乱码的问题
- Excel数据导入到数据库乱码问题
- JFreeChart 中文乱码的解决 绝对管用!
- 将DataGrid的数据直接导入Excel
- C#.NET 2003中,将DataGrid导出到Excel后,所有的中文显示为乱码
- DataGrid数据导入Excel
- DataGrid数据导入Excel
- rails 校验图片格式
- 深入SQl编程
- 你是职业人识吗
- Windows SharePoint Services 3.0
- LAMP安装配置手记
- Datagrid 导入到EXcel乱码的朋友,这里绝对解决
- JAVASCRIPT捕获IE的刷新按钮和关闭按钮
- 使用 JDBC 连接不同版本 DB2 数据库的兼容性问题
- csdn
- ASP.Net-----学习动态性能表 数据源控件 数据绑定控件
- msdn中的ginA例子中的WlxLoggedOutSAS
- [转载]WindowsXP 系统登陆原理及其验证机制概述
- 二维数组螺旋式打印算法
- [转载]手动清除DLL后门(DLL backdoor)