积极答复者
GridView导出Excel报错

问题
-
GridView导出Excel后页面报错,截图如下:
请教各位这是什么问题,如何解决?急!在线等。
zy- 已编辑 RickyLinModerator 2009年6月1日 3:06 帮助其显示图片
答案
全部回复
-
-
代码就是从以前同样功能的代码Copy过来的
#region DataTable导出Excel方法
private void Export(Page page, DataTable tab, string filename)
{
HttpResponse httpresponse = page.Response;
DataGrid datagrid = new DataGrid();
datagrid.DataSource = tab.DefaultView;
datagrid.AllowPaging = false;
datagrid.HeaderStyle.BackColor = Color.White;
datagrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
datagrid.HeaderStyle.Font.Bold = false;
datagrid.DataBind();
httpresponse.AppendHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8)); //filename="*.xls";
httpresponse.ContentEncoding = Encoding.GetEncoding("gb2312");
httpresponse.ContentType = "application/ms-excel";
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
datagrid.RenderControl(hw);//根据登录用户信息选择导出Excel文件路径和文件名
string filepath = string.Empty;
filepath = @"\\192.168.1.100\Excel" + "\\" + filename;
filepath = filepath.Replace("\\", "\\\\");StreamWriter sw = File.CreateText(filepath);
sw.Write(tw.ToString());
sw.Close();DownFile(httpresponse, filename, filepath);
httpresponse.End();
}private bool DownFile(HttpResponse response, string filename, string fullpath)
{
try
{
response.ContentType = "application/octet-stream";response.AppendHeader("content-disposition", "attachment;filename=" +
HttpUtility.UrlEncode(filename, Encoding.UTF8) + ";charset=gb2312");
FileStream fs = 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();
File.Delete(fullpath);return true;
}
catch
{
return false;
}
}
#endregion
zy -
改用如下代码试试是否可以。
public override void VerifyRenderingInServerForm(Control control) { //base.VerifyRenderingInServerForm(control); } protected override void Render(HtmlTextWriter writer) { Response.Clear(); Response.AppendHeader("content-disposition", "attachment;filename=ABC.xls"); Response.ContentType = "application/ms-excel"; gv.RenderControl(writer); //gv为你的表格控件 Response.End(); }
理解的越多,需要记忆的就越少 -
谢谢各位的帮助。问题解决。是:Response.ContentEncoding = System.Text.Encoding.UTF8;这段代码的问题。原来是UTF7,改UTF8就行了。但新的问题出来了。我在GridView外面套了一个UpdatePanel。然后就不能导了。我查了页面的原代码,发现加了UpdatePanel后在GridView外面多了一句代码,如下:<div style="overflow:auto;position:relative;width:100%;height:550px;">。现在报错就是跟这句代码有关。不知道能不能解决。希望大家帮我想想办法。谢谢先:)
报错的图片地址如下:
http://user.qzone.qq.com/66289055/photo/c89f2591-dbb1-49fd-88a7-e10b879dca43/M4S1nfX*p0bopqjkm*VyB.0I4DUK8ncAAA!!/
http://s17.photo.store.qq.com/http_imgload.cgi?/rurl4_b=d4d318d773278cf2b54df596bf3b45f9ecab11245dff3d267730e106e99dc665d7b491b6b7fd4bbd43021bf461b766247e1be95c327760710835d62e14a13c72ba0a13864d81a003ee096581564f625ec10f1102
zy