Asp.net (datatable) excle

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  ); Foreach (Datacolumn Column In  Sourcetable. columns) {headerrow. createcell (column. ordinal). setcellvalue (column. columnname );}  Int Rowindex = 1  ;  Foreach (Datarow row In  Sourcetable. Rows) {irow datarow = Sheet. createrow (rowindex );  Foreach (Datacolumn ColumnIn  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>           ///  Datatable export to excel file [winfrom]  ///   </Summary>           ///   <Param name = "dtsource">  Source able  </Param>           ///   <Param name = "strheadertext">  Header text </Param>           ///   <Param name = "strfilename">  Save location  </Param>           Public   Static   Void Export (datatable dtsource, String Strheadertext, String  Strfilename ){  Using (Memorystream MS = Export (dtsource, strheadertext )){ Using (Filestream FS = New  Filestream (strfilename, filemode. Create, fileaccess. Write )){  Byte [] DATA = Ms. toarray (); FS. Write (data,  0  , Data. Length); FS. Flush ();}}}  ///   <Summary>          ///  Export Excel [web] 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 ){  Try  {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 =  " + System. web. httputility. urlencode (filename, system. text. encoding. utf8); httpcontext. current. response. binarywrite (Ms. toarray (); httpcontext. current. response. end (); Ms. close (); MS = Null  ;}  Catch (Exception ex) {httpcontext. Current. response. Write (Ex );}} 

The plug-in npoi is used here.

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.