DataGrid数据导出excel文件,有时出现乱码解决方法。
来源:互联网 发布:vscode c 生成exe 编辑:程序博客网 时间:2024/05/02 02:10
以前是用Response.Write()方法直接将DataGrid内容输出到文件,但有时会出现excel文件里的中文全是乱码。
解决办法:
先将excel文件写到服务器上,然后再下载到客户端。
以下是代码片段:
private void Export(System.Web.UI.WebControls.DataGrid dg,string fileName,string typeName)
{
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("GB2312");
httpResponse.ContentType = typeName;
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=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;
}
}
private void btnWord_Click(object sender, System.EventArgs e)
{
Export(dataGrid,"FileName.doc","application/ms-word");//导出至Wrod
}
private void btnExcel_Click(object sender, System.EventArgs e)
{
Export(dataGrid,"FileName.xls","application/ms-excel");//导出至Excel
}
上面的是C#版本。下面贴出VB.net版本的,方便大家使用:
Private Function Export(ByVal dg As System.Web.UI.WebControls.DataGrid, ByVal fileName As String, ByVal typeName As String) As Boolean
Dim httpResponse As System.Web.HttpResponse = Page.Response
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312")
httpResponse.ContentType = typeName
Dim tw As System.IO.StringWriter = New System.IO.StringWriter
Dim hw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(tw)
dg.RenderControl(hw)
Dim filePath As String = Server.MapPath("..") + fileName
Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(filePath)
sw.Write(tw.ToString())
sw.Close()
DownFile(httpResponse, fileName, filePath)
httpResponse.End()
Return True
End Function
Private Function DownFile(ByVal Response As System.Web.HttpResponse, ByVal fileName As String, ByVal fullPath As String) As Boolean
Try
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ";charset=GB2312")
Dim fs As System.IO.FileStream = System.IO.File.OpenRead(fullPath)
Dim fLen As Long = fs.Length
Dim size As Integer = 102400 '//每100K同时下载数据
Dim readData() As Byte
ReDim readData(size) '//指定缓冲区的大小
If (size > fLen) Then
size = Convert.ToInt32(fLen)
End If
Dim fPos As Long = 0
Dim isEnd As Boolean = False
While (Not isEnd)
If ((fPos + size) > fLen) Then
size = Convert.ToInt32(fLen - fPos)
ReDim readData(size)
isEnd = True
End If
fs.Read(readData, 0, size) '//读入一个压缩块
Response.BinaryWrite(readData)
fPos += size
End While
fs.Close()
System.IO.File.Delete(fullPath)
Return True
Catch
Return False
End Try
End Function
Protected Sub btnWord_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnWord.Click
Export(Me.DataGrid1, "FileName.doc", "application/ms-word") '导出至Wrod
End Sub
Protected Sub btnExcel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExcel.Click
Export(Me.DataGrid1, "FileName.xls", "application/ms-excel") '导出至Excel
End Sub
- DataGrid数据导出excel文件,有时出现乱码解决方法。
- 在java读取文件时,有时会出现乱码,解决方法
- VC++ 导出DataGrid数据为Excel文件
- DataGrid - 导出Excel文件
- DataGrid - 导出Excel文件
- DataGrid 导出 Excel 中文乱码
- php导出EXCEL出现乱码的终极解决方法
- 在使用中文名导出数据到excel文件时文件名出现乱码的解决办法
- 在使用中文名导出数据到excel文件时文件名出现乱码的解决办法
- 如何将DataGrid中的数据导出到Excel文件中
- 如何将DataGrid中的数据导出到Excel文件中
- Datagrid数据导出到excel文件的三种方法
- Datagrid数据导出到excel文件的三种方法
- Datagrid数据导出到excel文件的三种方法
- datagrid数据导出到excel
- DataGrid数据导出到Excel
- OBIEE数据导出到excel图例文字出现方块乱码
- C# 中文数据导出CSV出现乱码解决方法
- 绘制平行线
- 我对【纹理颜色混合】与【ALPHA混合】的理解
- 经典对白,超然人生
- 上传文件 JSP+java
- C#通过Win32 API操作IE浏览器 --- 获得IE的URL
- DataGrid数据导出excel文件,有时出现乱码解决方法。
- java日期函数2
- 北京拟严控“双外生”进京 近九成青年表示反对
- 高效能人士的9种性格(转载)
- 关于字符串逆序的问题
- 开放封闭原则--OCP
- 进制转换(一)
- 炎龙骑士团-黄金城之迷剧情
- 防火墙基本配置任务列表