首页 新闻 搜索 专区 学院

GridView输出Excel问题

0
[已解决问题] 解决于 2008-07-15 08:41
&nbsp;&nbsp; 今天在一个ASP.NET项目使用Gridview的方式输出Excel到客户端,代码如下:<br>&nbsp; <br><div class="cnblogs_code"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 128, 128);">&nbsp;1</span><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Buffer&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">;<br></span><span style="color: rgb(0, 128, 128);">&nbsp;2</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">Response.ClearContent();<br></span><span style="color: rgb(0, 128, 128);">&nbsp;3</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">指定http名称和值&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 128, 128);">&nbsp;4</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.AddHeader(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">content-disposition</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">attachment;&nbsp;filename=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;HttpUtility.UrlEncode(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">test</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;System.Text.Encoding.UTF8)&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">.xls</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br></span><span style="color: rgb(0, 128, 128);">&nbsp;5</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">指定文件类型</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 128, 128);">&nbsp;6</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.ContentType&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">application/excel</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;<br></span><span style="color: rgb(0, 128, 128);">&nbsp;7</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.ContentEncoding&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;System.Text.Encoding.GetEncoding(</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">GB2312</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br></span><span style="color: rgb(0, 128, 128);">&nbsp;8</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;StringWriter&nbsp;sw&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;StringWriter();<br></span><span style="color: rgb(0, 128, 128);">&nbsp;9</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExcelGridView.AllowPaging&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br></span><span style="color: rgb(0, 128, 128);">10</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.ExcelGridView.DataSource&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;dtStudentScore;<br></span><span style="color: rgb(0, 128, 128);">11</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExcelGridView.DataBind();<br></span><span style="color: rgb(0, 128, 128);">12</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">HtmlTextWriter&nbsp;输出流</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 128, 128);">13</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HtmlTextWriter&nbsp;htw&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;HtmlTextWriter(sw);<br></span><span style="color: rgb(0, 128, 128);">14</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExcelGridView.RenderControl(htw);<br></span><span style="color: rgb(0, 128, 128);">15</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);">&nbsp;style&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">&lt;style&gt;&nbsp;.text&nbsp;{&nbsp;mso-number-format:\@;&nbsp;}&nbsp;&lt;/script&gt;&nbsp;</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">单元格式为文本格式<br></span><span style="color: rgb(0, 128, 128);">16</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">sw写入到http输出流</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 128, 128);">17</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Output.Write(style);<br></span><span style="color: rgb(0, 128, 128);">18</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Output.Write(sw.ToString());<br></span><span style="color: rgb(0, 128, 128);">19</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.Flush();<br></span><span style="color: rgb(0, 128, 128);">20</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Response.End();<br></span><span style="color: rgb(0, 128, 128);">21</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ExcelGridView.Visible&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">false</span><span style="color: rgb(0, 0, 0);">;<br>在客户端输出Excel后,在点击原页面上任何按钮,页面没有任何反应,请问这是为什么?<br></span></div>
吴畏的主页 吴畏 | 菜鸟二级 | 园豆:426
提问于:2008-07-11 16:02
< >
分享
最佳答案
0
@无畏 你给我发封邮件吧,我写了一个excel导入/导入的类,在我的项目中应用没有问题.我可以发给你参考. 你说的这个问题,我觉得只有在实际的环境中调试才能发现,在这里看到.
张荣华 | 老鸟四级 |园豆:2020 | 2008-07-11 18:32
其他回答(3)
0
换个浏览器试一下,看看是否是浏览器的问题。
玉开 | 园豆:8822 (大侠五级) | 2008-07-11 16:30
0
gridview生成xsl文件,网上太多了啊 http://blogs.claritycon.com/blogs/steve_holstad/archive/2006/07/02/1131.aspx 一老外的blog 不错的,去看看吧lz
BeautyLogic | 园豆:220 (菜鸟二级) | 2008-07-11 21:34
0
无畏 你看看我这篇博客吧! 这个是可以的。 http://www.cnblogs.com/morningwang/archive/2007/05/30/765041.html
金鱼 | 园豆:1090 (小虾三级) | 2008-07-13 12:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册