首页 > 代码库 > 粘贴板与导出Excel

粘贴板与导出Excel

网页代码

 1 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"> 2  3        <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server"> 4             </asp:ScriptManagerProxy> 5       <asp:UpdatePanel id="UpdatePanel1" runat="server"> 6          <Triggers >  //需要加这个来分析从服务器接受的消息,即对应网页报错:The message received from the serber could not be parsed 7           <asp:PostBackTrigger ControlID ="ButtonExportExcell"/> 8          </Triggers> 9         10         <contenttemplate>11              <asp:Button ID="ButtonCopyData" runat="server" BackColor="Silver" 12                 BorderStyle="Solid" BorderWidth="1px" 13                 style="z-index: 1; left: 573px; top: 162px; position: absolute; height: 24px; width: 97px; " 14                 onclientclick="copyData(‘ContentPlaceHolder1_BaseGridView‘);"  Text="复制到粘贴板" />15 16              <asp:Button ID="ButtonExportExcell" runat="server" BackColor="Silver" 17                 BorderStyle="Solid" BorderWidth="1px"  OnClick="ButtonExportExcell_Click"18                 style="z-index: 1; left: 678px; top: 162px; position: absolute; height: 24px; width: 97px; " 19                 Text="导出Excel" />20             21          <asp:GridView ID="BaseGridView" runat="server" AllowPaging="False" 22                 BackColor="Gray" BorderColor="White" 23                 BorderStyle="Ridge" BorderWidth="2px" CaptionAlign="Left" CellPadding="3" 24                 CellSpacing="1" Font-Overline="False" Font-Size="Small" Font-Underline="False" 25                 GridLines="None" PageSize="3" 26                 style="Z-INDEX: -1; LEFT: 24px;   POSITION: absolute; TOP: 211px;   width: auto; height: 13px; " 27                 UseAccessibleHeader="False" AutoGenerateColumns="false">28                 <PagerSettings Position="TopAndBottom" />29                 <RowStyle BackColor="#DEDFDE" BorderColor="Black" Font-Size="Small" 30                     ForeColor="Black" HorizontalAlign="Left" VerticalAlign="Top" Wrap="True" />31                 <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />32                 <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Left" 33                     Wrap="True" />34                 <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />35                 <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" Font-Size="Small" 36                     ForeColor="#E7E7FF" HorizontalAlign="Left" VerticalAlign="Top" Wrap="False" />37                                     38                 <Columns>39                      <asp:TemplateField HeaderText="零件号">40                             <ItemTemplate>41                             <asp:Label ID="LabelPartDesc" runat="server" Width="120px" Text=<%# Bind("零件号") %> ToolTip=<%# Bind("零件号") %> ></asp:Label>42                             </ItemTemplate>43                     </asp:TemplateField>44                     <asp:TemplateField HeaderText="条形码" >45                             <ItemTemplate>46                             <asp:Label ID="LabelBarCode" runat="server" Width="120px" Text=<%# Bind("条形码") %> ToolTip=<%# Bind("条形码") %> ></asp:Label>47                             </ItemTemplate>48                     </asp:TemplateField>49 50                 </Columns>51                                     52                 <EditRowStyle Wrap="True" />53             </asp:GridView>54 55 56          </contenttemplate>57        </asp:UpdatePanel>58      59     <script type="text/javascript" src=http://www.mamicode.com/"Scripts/My97DatePicker/WdatePicker.js">60        </script> 61 62         <script type="text/javascript">63 64              function copyData(TempID) {65                  var objFrame = document.getElementById(TempID);66                  var txt = document.body.createTextRange();67                  if (objFrame != null) {68                      txt.moveToElementText(objFrame);69                      //////txt.select();70                      txt.execCommand("copy", "", null); // 复制71                  }72              }73 74          </script>75 76  </asp:Content>

 

 

 1 //-----后台代码-------- 2  public void ExcelOut(GridView gv) 3         { 4             if (gv.Rows.Count > 0) 5             { 6                 Response.Clear(); 7                 Response.ClearContent(); 8                 Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"); 9                 Response.Charset = "gbk";10                 Response.ContentEncoding = System.Text.Encoding.GetEncoding("gbk");11                 Response.ContentType = "application/ms-excel";12                 StringWriter sw = new StringWriter();13                 HtmlTextWriter htw = new HtmlTextWriter(sw);14                 gv.RenderControl(htw);15                 Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=gbk\">");16                 this.EnableViewState = false;17                 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();18                 HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);19                 gv.RenderControl(oHtmlTextWriter);20                 Response.Write(oStringWriter.ToString());21                 Response.End();22             }23             else24             {25                 Response.Write("没有数据");26             }27         }28         public override void VerifyRenderingInServerForm(Control control)29         {30         //要导出数据,需要重载此方法。31             //base.VerifyRenderingInServerForm(control);32         }33 34         protected void ButtonExportExcell_Click(object sender, EventArgs e)35         {36             ExcelOut(BaseGridView);37         }

 

-----------------------------------------------------------

从GridView导出到Excel时报错:类型GridView的控件GridView1必须放置在具有runat=server的窗体标记内

但要注意的是在以前的vs.net2003时,就直接行了,在vs.net2005中还的加下一下语句,重载VerifyRenderingInServerForm方法:

public override void VerifyRenderingInServerForm(Control control)    

{         

  //空过程    

}

 MSDN上的 VerifyRenderingInServerForm 方法的描述:   必须位于 <form runat=server> 标记中的控件可以在呈现之前调用此方法,以便在控件被置于标记外时显示错误信息。发送回或依赖于注册的脚本块的控件应该在 Control.Render 方法的重写中调用此方法。呈现服务器窗体元素的方式不同的页可以重写此方法以在不同的条件下引发异常。   如果回发或使用客户端脚本的服务器控件没有包含在 HtmlForm 服务器控件 (<form runat="server">) 标记中,它们将无法正常工作。这些控件可以在呈现时调用该方法,以在它们没有包含在 HtmlForm 控件中时提供明确的错误信息。   开发自定义服务器控件时,通常在为任何类型的输入标记重写 Render 方法时调用该方法。这在输入控件调用 GetPostBackEventReference 或发出客户端脚本时尤其重要。复合服务器控件不需要作出此调用。

 

-----------------------------------------------------------

网页报错:The message received from the serber could not be parsed

无法分析从服务器收到的消息。之所以出现此错误,常见的原因是: 在通过调用 Response.Write() 修改响应时,将启用响应筛选器、HttpModule 或服务器跟踪。 今天在Ajax的UpdatePanel下实现 Export Excel或是Word时,它即出现error:

行: 4723 错误: Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息。之所以出现此错误,常见的原因是: 在通过调用 Response.Write() 修改响应时,将启用响应筛选器、HttpModule 或服务器跟踪。 详细信息: 分析附近的“<div>  <table cells”时出错。

确认Ajax下无数据导出时,会出现上面异常。网上查询,并花费多时,无得到满意的解决方案。

只有自己手动添加下面code,即可:

</ContentTemplate> < Triggers>             <asp:PostBackTrigger ControlID="btnExportWord" />             <asp:PostBackTrigger ControlID="btnExportExcel" />  </Triggers>  </asp:UpdatePanel>

粘贴板与导出Excel