[已解决问题]
解决于 2008-07-15 08:41
今天在一个ASP.NET项目使用Gridview的方式输出Excel到客户端,代码如下:<br> <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);"> 1</span><img src="/Images/OutliningIndicators/None.gif" align="top"><span style="color: rgb(0, 0, 0);"> Response.Buffer </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </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);"> 2</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> </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);"> 3</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">指定http名称和值 </span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 128, 128);"> 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);"> 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);">, </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">attachment; filename=</span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> 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);">, System.Text.Encoding.UTF8) </span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);"> </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);"> 5</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> </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);"> 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);"> Response.ContentType </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </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);"> 7</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> Response.ContentEncoding </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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);"> 8</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> StringWriter sw </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> StringWriter();<br></span><span style="color: rgb(0, 128, 128);"> 9</span><span style="color: rgb(0, 0, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> ExcelGridView.AllowPaging </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </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"> </span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">.ExcelGridView.DataSource </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> 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"> 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"> </span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">HtmlTextWriter 输出流</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);"> HtmlTextWriter htw </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);"> 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"> 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"> </span><span style="color: rgb(0, 0, 255);">string</span><span style="color: rgb(0, 0, 0);"> style </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);"><style> .text { mso-number-format:\@; } </script> </span><span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(0, 0, 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);">16</span><span style="color: rgb(0, 128, 0);"><img src="/Images/OutliningIndicators/None.gif" align="top"> </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);"> 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"> 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"> 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"> 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"> ExcelGridView.Visible </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </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