Export data to excel (Standard Excel)

Source: Internet
Author: User
       Private   Static Stream exportdatatabletoexcel (datatable sourcetable, String  Sheetname) {hssfworkbook workbook = New  Hssfworkbook (); memorystream MS = New  Memorystream (); isheet Sheet = Workbook. createsheet (sheetname); irow headerrow = Sheet. createrow ( 0  ); //  Handling header.              Foreach (Datacolumn Column In  Sourcetable. columns) headerrow. createcell (column. ordinal). setcellvalue (column. columnname );  //  Handling Value.              Int Rowindex = 1  ;  Foreach (Datarow row In Sourcetable. Rows) {irow datarow = Sheet. createrow (rowindex );  Foreach (Datacolumn Column In  Sourcetable. columns) {datarow. createcell (column. ordinal). setcellvalue (row [column]. tostring ();} rowindex ++ ;} Workbook. Write (MS); Ms. Flush (); Ms. Position = 0  ; Sheet = Null ; Headerrow = Null  ; Workbook = Null  ;  Return  MS ;}  ///   <Summary>          ///  Export EXCEL from datatable  ///   </Summary>          ///   <Param name = "sourcetable"> Datatable to export data  </Param>          ///   <Param name = "FILENAME">  Excel worksheet name  </Param>          ///   <Returns>  Excel worksheet  </Returns>          Public   Static   Void Exportdatatabletoexcel (datatable sourcetable, String Filename,String  Sheetname) {memorystream MS = Exportdatatabletoexcel (sourcetable, sheetname) As  Memorystream; httpcontext. Current. response. contentencoding = System. Text. encoding. getencoding ( "  UTF-8  "  ); Httpcontext. Current. response. appendheader (  "  Content-Disposition  " , " Attachment; filename =  " + Filename); httpcontext. Current. response. binarywrite (Ms. toarray (); httpcontext. Current. response. End (); Ms. Close (); MS = Null  ;} 

 

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.