好久沒有寫東西,現在貼一下這個,希望對要用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); } }}