ASP. NET Excel export class

Source: Internet
Author: User

 

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 );}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.