datagridview用户控件打入excel
时间:2011-04-07 来源:itcfj
   
前端页面:
  <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
  <html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
  <title>Untitled Page</title>
  
  
  </head>
  <body>
      <form id="form1" runat="server">
      <div>
      <div id="print" runat="server">
      <style type="text/css">
          td
          {
              background-color:Gray;
              font-size:large
          }
          .style1
          {
              width: 143px;
          }
      </style>
        <table id="table1" border="1">
            <tr  style=" background-color:Red">
                <td>用户名</td>
                <td class="style1" rowspan="2">密  码22</td>
            </tr>
            <tr>
                <td>22</td>
            </tr>
            </table>
      </div>
     
      <asp:Button ID="Btn_Export" runat="server" Text="Export to Excel (Default)" OnClick="Btn_ExportClick" />
     
     
      <asp:Button ID="Btn_ExportToExcelPaging" runat="server" Text="Export to Excel Paging" OnClick="Btn_ExportExcelPaging" />
     
      </div>
      </form>
  </body>
  </html>
后台代码:
  
      protected void Btn_ExportClick(object sender, EventArgs e)
      {
  
          ToExcel(print, "dd", true);
         // string style = @"<style> .text { mso-number-format:\@; } </script> ";
  
         // Response.ClearContent();
         // Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
         // Response.ContentType = "application/excel";
  
         // StringWriter sw = new StringWriter();
         // HtmlTextWriter htw = new HtmlTextWriter(sw);
         // print.RenderControl(htw);
         //// gvUsers.RenderControl(htw);
         // // Style is added dynamically
         // Response.Write(style);
         // Response.Write(sw.ToString());
         // Response.End();
      }
  
      protected void Btn_ExportExcelPaging(object sender, EventArgs e)
      {
          //DisableControls(gvUsers);
  
          //Response.ClearContent();
          //Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
          //Response.ContentType = "application/excel";
  
          //StringWriter sw = new StringWriter();
          //HtmlTextWriter htw = new HtmlTextWriter(sw);
  
          //gvUsers.RenderControl(htw);
          //Response.Write(sw.ToString());
          //Response.End();      
      }
      public override void VerifyRenderingInServerForm(Control control)
      {
  
      }
      public  void ToExcel(Control control, string fileName, bool unformat)
      {
          System.IO.StringWriter tw = new System.IO.StringWriter();
          System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
          control.RenderControl(hw);
  
          System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
  
          Response.Write(control.ToString());
          Response.Clear();
          Response.Charset = "UTF-8";
          Response.ContentEncoding = System.Text.Encoding.Default;
          Response.ContentType = "application/vnd.ms-excel";
          Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName) + ".xls");
          Response.Write("<html><head><META http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\"><meta name=ProgId content=Excel.Sheet><meta name=Generator content=\"Microsoft Excel 11\"></head><body>");
          string output = tw.ToString();
          if (unformat)
          {
              // 2007-12-17 qianwt 用正则表达式去除HTML中的链接格式、图片格式
              output = Regex.Replace(output, @"<\s*/?\s*a[^>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
              output = Regex.Replace(output, @"<\s*/?\s*img[^>]*>", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
          }
          Response.Write(output);
          Response.Write("</body></html>");
          Response.End();
          hw.Close();
          hw.Flush();
          tw.Close();
          tw.Flush();
      }
  }










