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 ).