求解:GridView导成Excel没内容或者是乱码

heheerwa 2010-10-02 06:26:49
代码
protected void btnport_Click(object sender, EventArgs e)
{
ToExcel("application/ms-excel", "资产汇总表" + DateTime.Now.ToString("yy-MM") + ".xls");
//ToExcelTest(this.GridView1, "资产汇总表" + DateTime.Now.ToString("yy-MM") + ".xls");
}
private void ToExcel(string fileType, string fileName)
{
this.GridView1.AllowPaging = false;
BindGrid();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString());
Response.ContentType = fileType;
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.RenderControl(hw);
Response.Write(tw.ToString());
Response.End();


}

我还在网上copy的很多个方法来试都不行
部分乱码
+ADw-div+AD4- +ADw-table cellspacing+AD0AIg-0+ACI- rules+AD0AIg-all+ACI- border+AD0AIg-1+ACI- id+AD0AIg-ctl00+AF8-ChildMaster+AF8-GridView1+ACI- style+AD0AIg-border-collapse:collapse+ADsAIgA+- +ADw-tr+AD4- +ADw-th scope+AD0AIg-col+ACIAPpDolegAPA-K14/th+AD4APA-th scope+AD0AIg-col+ACIAPlQNefAAPA-/th+AD4APA-th scope+AD0AIg-col+ACIAPnUokBQAPA-/th+AD4APA-th scope+AD0AIg-col+ACIAPl9SXF58+37fADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPlTBckwAPA-/th+AD4APA-th scope+AD0AIg-col+ACIAPleLU/cAPA-/th+AD4APA-th scope+AD0AIg-col+ACIAPou+WQdR+lOCfxZT9wA8-/th+AD4APA-th scope+AD0AIg-col+ACIAPml8W4cAPA-/th+AD4APA-th scope+AD0AIg-col+ACIAPml8XEIAPA-/th+AD4APA-th scope+AD0AIg-col+ACIAPnnRW6RUDXnwADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPluJiMVl9pX0ADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPo0tf26NOXUoADw-/th+AD4APA-th scope+AD0AIg-col+ACIAPnnfdSiNOQA8-/th+AD4- +ADw-/tr+AD4APA-tr+AD4- +ADw-td+AD4-test2+ADw-/td+AD4APA-td+AD5T8F8PZzoAPA-/td+AD4APA-td+AD5b/IIqADw-/td+AD4APA-td+AD5b/IIqfPt+3wA8-/td+AD4APA-td+AD4-ACER+ADw-/td+AD4APA-td+AD4-Aspire SA10+ADw-/td+AD4APA-td+AD4-PTS170C01752104506E+ACo-02+ADw-/td+AD4APA-td+AD5QGWc6aXwAPA-/td+AD4APA-td+AD4-8+ADw-/td+AD4APA-td+AD4-test2+ADw-/td+AD4APA-td+AD4-2010/9/28 0:00:00+ADw-/td+AD4APA-td+AD4-0+ADw-/td+AD4APA-td+AD4-15+ADw-/td+AD4- +ADw-/tr+AD4APA-tr+AD4- +ADw-td+AD4-test2+ADw-/td+AD4APA-td+AD6NhX6nexSLsGcsADw-/td+AD4APA-td+AD4-test+ADw-/td+AD4APA-td+AD6QGk/hfPt+3wA8-/td+AD4APA-td+AD4-test+ADw-/td+AD4APA-td+AD4-test+ADw-/td+AD4APA-td+AD4-test+ADw-/td+AD4APA-td+AD5QGWc6aXwAPA-/td+AD4APA-td+AD4-8+ADw-/td+AD4APA-td+AD4-test2+ADw-/td+AD4APA-td+AD4-2010/9/28 0:00:00+ADw-/td+AD4APA-td+AD4-12222+ADw-/td+AD4APA-td+AD4-50+ADw-/td+AD4- +ADw-/tr+AD4APA-tr+AD4- +ADw-td+AD5UCIuhADw-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4AJg-nbsp+ADsAPA-/td+AD4APA-td+AD4-12222+ADw-/td+AD4APA-td+AD4-65+ADw-/td+AD4- +ADw-/tr+AD4- +ADw-/table+AD4- +ADw-/div+AD4-
...全文
137 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
heheerwa 2010-10-07
  • 打赏
  • 举报
回复
个人认为可能是浏览器的问题,因为我在xp系统中用的是Office2003,ie6就能正常导出了~
heheerwa 2010-10-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
修改编码
<globalization responseEncoding="utf-8" requestEncoding="utf-8"/>
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(File……
[/Quote]
用了你的,能导出来了,不过导出来的都是html代码,并不是Excel数据。我想是不是跟系统的Excel有关系,我装的是Office 2007
导出结果:
<div>
<table cellspacing="0" rules="all" border="1" id="ctl00_ChildMaster_GridView1" style="border-collapse:collapse;">
<tr>
<th scope="col">部门</th><th scope="col">名称</th><th scope="col">用途</th><th scope="col">归属系统</th><th scope="col">品牌</th><th scope="col">型号</th><th scope="col">设备出厂编号</th><th scope="col">楼宇</th><th scope="col">楼层</th><th scope="col">科室名称</th><th scope="col">安装时间</th><th scope="col">购置费用</th><th scope="col">租用费</th>
</tr><tr>
<td>test2</td><td>台式机</td><td>导航</td><td>导航系统</td><td>ACER</td><td>Aspire SA10</td><td>PTS170C01752104506E*02</td><td>候机楼</td><td>8</td><td>test2</td><td>2010/9/28 0:00:00</td><td>0</td><td>15</td>
</tr><tr>
<td>test2</td><td>超级笔记本</td><td>test</td><td>通信系统</td><td>test</td><td>test</td><td>test</td><td>候机楼</td><td>8</td><td>test2</td><td>2010/9/28 0:00:00</td><td>12222</td><td>50</td>
</tr><tr>
<td>合计</td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td> </td><td>12222</td><td>65</td>
</tr>
</table>
</div>
wuyq11 2010-10-02
  • 打赏
  • 举报
回复
修改编码
<globalization responseEncoding="utf-8" requestEncoding="utf-8"/>
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8)+".xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
空内容设置表头
heheerwa 2010-10-02
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mengxj85 的回复:]
引用 2 楼 heheerwa 的回复:
引用 1 楼 wuyq11 的回复:
修改编码
<globalization responseEncoding="utf-8" requestEncoding="utf-8"/>
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment……
[/Quote]
网上一搜“GridView导Excel”基本都是这段代码~
mengxj85 2010-10-02
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 heheerwa 的回复:]
引用 1 楼 wuyq11 的回复:
修改编码
<globalization responseEncoding="utf-8" requestEncoding="utf-8"/>
Response.Charset = "utf-8";
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtil……
[/Quote]
用这个代码直接导出来的本来就不是真正的EXCEL格式,是HTML,或者要换成EXCEL格式,可以在网上找一下代码,或者引用别人的类库,比较复杂

62,271

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧