Asp.net export execl to solve Chinese name garbled characters

Source: Internet
Author: User

1. Export to CVS

 First, you need to obtain a series of string types, and then determine the export format... first obtain the table header. Whether you are fixed or dynamic, add  "  ,  "  To indicate a column. environment. newline is added to the last line. For example, arraylist = New  Arraylist (); list. Add (  "  Xxx  "  ); List. Add (  "  Xx  " ); List. Add (  "  Xx  "  ); List. Add (  "  Xx  "  ); List. Add (  "  Xxx  "  ); List. Add (  "  Xx  " ); List. Add (  "  Xx  "  ); List. Add (  "  Xx  "  ); List. Add (  "  Xxxx  "  );  String [] Saleheaders = ( String []) List. toarray (Typeof ( String  ); Temp = Salewriteoffhtmlstr (saledt, saleheaders );  Byte [] Buffer = System. Text. encoding. utf8.getbytes (temp. tostring ());  Byte [] Outbuffer = New   Byte [Buffer. Length + 3  ]; Outbuffer [  0 ] = (Byte ) 0xef  ; Outbuffer [  1 ] = ( Byte ) 0xbb  ; Outbuffer [  2 ] = ( Byte ) 0xbf  ; Array. Copy (buffer,  0 , Outbuffer, 3 , Buffer. Length); response. Write (encoding. utf8.getstring (outbuffer); response. Buffer = True  ; Response. addheader (  "  Content-Disposition  " , "  Attachment; filename =  " + Server. urlencode ( "  Sales data  " ) + " . CSV;  "  ); Response. charset = "  UTF-8  "  ; Response. contenttype = "  Text/CSV  "  ;  Public   String Salewriteoffhtmlstr (datatable DT, String [] Headers ){StringHtmlstr ="";
Foreach ( String S In Headers) {htmlstr + = S. tostring (). Replace ( ' , ' , ' , ' ) + " , " ;} Htmlstr = Htmlstr. substring ( 0 , Htmlstr. Length- 1 ) + Environment. newline; Foreach (Datarow Dr In DT. Rows) {htmlstr + = Dr [ " Base_tabletnumber " ]. Tostring (). Replace ( ' , ' , ' , ' ) + " , " ;... Htmlstr + = Environment. newline ;} Return Htmlstr ;}

2. Export to xls

 Httpresponse resp; Resp = Page. response; resp. contentencoding = System. Text. encoding. getencoding ("  Gb2312  "  ); Resp. appendheader (  "  Content-Disposition  " , "  Attachment; filename =  " + System. Web. httputility. urlencode ( "  Sales table  " , System. Text. encoding. utf8) + "  . Xls; "  ); Resp. Write (temp); temp = ""  ; Resp. End ();  Public   String Salewriteoffhtmlstr (datatable DT, String  [] Headers ){  String Htmlstr = ""  ;  Foreach ( String SIn  Headers) {htmlstr + = S. tostring (). Replace ( '  ,  ' , '  ,  ' ) + "  \ T  "  ;} Htmlstr = Htmlstr. substring ( 0 , Htmlstr. Length- 1 ) +"  \ N  "  ;  Foreach (Datarow Dr In  DT. Rows) {htmlstr + = Dr [ "  Base_tabletnumber  " ]. Tostring (). Replace ( '  ,  ' , '  , ' ) + "  \ T  "  ;.......................................... Htmlstr + = "  \ N  "  ;}  Return  Htmlstr ;} 

System. Web. httputility. urlencode ("Sales table", System. Text. encoding. utf8 ).

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.