I haven't written anything for a long time. Now I am posting this article. I hope it will be helpful for anyone who wants to use ASP. NET to export excel. I also hope you can improve it, but don't forget to tell me: 0>
Latest Excel exported by ASP. NET
Views: ASP. NET pages and DataGrid (webcontrol) data can be exported, and single table headers can be exported.
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 {///// abstract description of Excel. /// Public class Excel {public Excel () {// todo: add the constructor logic here //} 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" + myexcelheader + "\ 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 ();}/* Get form headers * // * Form headers are composed of tables, which are arranged occasionally, add runat = server */Public String tableheader (htmltable tab) to table in HTML {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) // control data of the even item (currently only textbox and dropdownlist, without 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 );}}}