ASP.NET匯出EXCEL類

來源:互聯網
上載者:User

 

好久沒有寫東西,現在貼一下這個,希望對要用ASP.NET匯出EXCEL的朋友,有所協助,也希望大家改進,不過不要忘記告訴我哦:0>

最新ASP.NET匯出EXCEL類

說明:可以匯出ASP.NET頁面和DATAGRID(WebControl)資料,可以匯出表單頭

 

using System;using System.Data;using System.Text;using System.Web;using System.Web.UI;using System.Diagnostics;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls;using System.Data.SqlClient;using System.Collections;namespace bookstore{ ///  /// Excel 的摘要說明。 ///  public class Excel {  public Excel()  {   //   // TODO: 在此處添加建構函式邏輯   //  }    public void SaveToExcel(Page myPage, DataTable dt,DataGrid DG,string myExcelHeader,HtmlTable Tab,string myFileName)  {   HttpResponse resp;       resp=myPage.Response;   resp.ContentEncoding=Encoding.GetEncoding("GB18030");   resp.AppendHeader("Content-Disposition","attachment;filename="+myFileName+".xls");   resp.ContentType="application/ms-excel";   string colHeaders = "\t\t\t\t"+ myExcelHeader +"\n\n\n";   colHeaders+=tableHeader(Tab)+"\n";   StringBuilder sb=new StringBuilder();      int mycol=DG.Columns.Count;   ArrayList myAL=new ArrayList();   for(int i=0;i   {    colHeaders +=DG.Columns[i].HeaderText+"\t";      myAL.Add(((System.Web.UI.WebControls.BoundColumn)(DG.Columns[i])).DataField);     }   colHeaders += "\n";   sb.Append(colHeaders);   int myrow=dt.Rows.Count;   for(int k=0;k   {    foreach(string field in myAL)    {        sb.Append(dt.Rows[k][field]);     sb.Append("\t");    }    sb.Append("\n");   }             colHeaders=sb.ToString();   colHeaders=colHeaders+"\n";   resp.Write(colHeaders);     resp.End();   resp.Clear();   resp.Close();    }  /*得到表單頭子*/  /*表單頭子有TABLE組成,偶次項排列,TABLE在HTML中加 RUNAT=SERVER*/  public string tableHeader(HtmlTable Tab)  {   int iCols=Tab.Rows[0].Cells.Count;   int iRows=Tab.Rows.Count;   string str="";      for(int row=0;row   {    for(int col=0;col    {     if(col%2==1)//取偶次項的控制項資料(目前只有TextBox和DropDownList,沒有包含LABEL)     {      try      {       if(Tab.Rows[row].Cells[col].Controls[0].ToString()=="System.Web.UI.LiteralControl")       {        if(Tab.Rows[row].Cells[col].Controls[1].ToString()=="System.Web.UI.WebControls.TextBox")        {         str+=((System.Web.UI.WebControls.TextBox)(Tab.Rows[row].Cells[col].Controls[1])).Text+"\t";        }        if(Tab.Rows[row].Cells[col].Controls[1].ToString()=="System.Web.UI.WebControls.DropDownList")        {         str+=((System.Web.UI.WebControls.ListControl)(((System.Web.UI.WebControls.DropDownList)((Tab.Rows[row].Cells[col].Controls[1]))))).SelectedValue+"\t";             }       }       else       {        if(Tab.Rows[row].Cells[col].Controls[0].ToString()=="System.Web.UI.WebControls.TextBox")        {         str+=((System.Web.UI.WebControls.TextBox)(Tab.Rows[row].Cells[col].Controls[0])).Text+"\t";        }        if(Tab.Rows[row].Cells[col].Controls[0].ToString()=="System.Web.UI.WebControls.DropDownList")        {         str+=((System.Web.UI.WebControls.ListControl)(((System.Web.UI.WebControls.DropDownList)((Tab.Rows[row].Cells[col].Controls[0]))))).SelectedValue+"\t";             }       }      }      catch      {       str+=Tab.Rows[row].Cells[col].InnerHtml+"\t";      }      if((col+1)%iCols==0)      {       str+="\n";      }     }     else     {      str+="\t"+Tab.Rows[row].Cells[col].InnerHtml+"\t";     }    }   }   return(str);  }   }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.