我在做gridview导出excel,我是引用别人的方法,别人的都正常,我下载后的就是乱码,可穿进去的时候显示是正常的,请问应该在哪儿修改一下呢? /// <summary>
/// 导出成Excel
/// </summary>
/// <param name="page">要导出的页面.如:this.Page</param>
/// <param name="tab">包含数据的DataTable</param>
/// <param name="FileName">要保存的文件名,如test.xls</param>
public static void Export(System.Web.UI.Page page, System.Data.DataTable tab, string FileName)
{
System.Web.HttpResponse httpResponse = page.Response;
System.Web.UI.WebControls.DataGrid dataGrid = new System.Web.UI.WebControls.DataGrid();
dataGrid.DataSource = tab.DefaultView;
dataGrid.AllowPaging = false;
dataGrid.HeaderStyle.BackColor = System.Drawing.Color.Transparent;
dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dataGrid.HeaderStyle.Font.Bold = true;
dataGrid.DataBind();
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); //filename="*.xls";
httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8") ;
httpResponse.ContentType = "application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
dataGrid.RenderControl(hw); string filePath = page.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 static 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=GB2312");
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;
}
}
/// 导出成Excel
/// </summary>
/// <param name="page">要导出的页面.如:this.Page</param>
/// <param name="tab">包含数据的DataTable</param>
/// <param name="FileName">要保存的文件名,如test.xls</param>
public static void Export(System.Web.UI.Page page, System.Data.DataTable tab, string FileName)
{
System.Web.HttpResponse httpResponse = page.Response;
System.Web.UI.WebControls.DataGrid dataGrid = new System.Web.UI.WebControls.DataGrid();
dataGrid.DataSource = tab.DefaultView;
dataGrid.AllowPaging = false;
dataGrid.HeaderStyle.BackColor = System.Drawing.Color.Transparent;
dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dataGrid.HeaderStyle.Font.Bold = true;
dataGrid.DataBind();
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); //filename="*.xls";
httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8") ;
httpResponse.ContentType = "application/ms-excel";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
dataGrid.RenderControl(hw); string filePath = page.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 static 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=GB2312");
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;
}
}
解决方案 »
- ExecuteNonQuery 要求已打开且可用的连接。连接的当前状态为已关闭。
- ASP.NET中实现FTP下载,在本机调试都可以下载,但发布项目到别的机子上就不行了
- 求分页的查询语句
- TextBox中,用输输入了javascript脚本怎么办?
- 将DataSet中的数据增加到Excel中时总是报“标准表达式中数据类型不匹配”的错
- 请问.net有没有获得当前访问者系统登陆用户的用户名的代码?
- 这段代码没看懂,再系统框架里面的东东
- 客户端获取listbox当前选定的值
- 请问各位高手,我现在要在页面上直接show图表,并且点击各点的时候还要能显示和该点有关的信息..
- 关于脚本与代码的通信问题。。。
- 如何对比IP 数据
- vs2008解决方案中两个web项目互相访问问题 特急
设置System.Text.Encoding.GetEncoding("UTF-8")