Export webform data to excel

Source: Internet
Author: User

In the morning, a function is added to the Order Management Section of the system to export the queried order information to excel for analysis by the Administrator. Previously writtenCodeAs follows:

Response. Clear ();
Response. bufferoutput
=   True ;
Response. charset
=   " Gb2312 " ;
Response. appendheader (
" Content-Disposition " , " Attachment; filename = filename.xls " );
Response. contentencoding
= Encoding. getencoding ( " Gb2312 " );
Response. contenttype
=   " Application/MS-Excel " ;
This . Enableviewstate =   False ;
System. Globalization. cultureinfo myinfo
=   New System. Globalization. cultureinfo ( " ZH-CN " , True );
Stringwriter OSW
=   New Stringwriter (myinfo );
Htmltextwriter ohtw
=   New Htmltextwriter (OSW );
Dgqueryresult. rendercontrol (ohtw );
Response. Write (OSW );
Response. End ();


My query uses paging. The customer requires that all the queried data be exported to excel. Therefore, this function cannot be implemented by exporting the control content to excel as before, so I changed the code to the following:

Response. Clear ();
Response. bufferoutput
=   True ;
Response. charset
=   " Gb2312 " ;
Response. appendheader (
" Content-Disposition " , " Attachment; filename = order .xls " );
Response. contentencoding
= System. Text. encoding. getencoding ( " Gb2312 " );
Response. contenttype
=   " Application/MS-Excel " ;
String Strexcelheader =   String . Empty;
String Strexcelitems;
If (Viewstate [ " SQL " ] ! =   Null ) // Obtain the SQL statement used for the preceding Query
{
// Get the titles of each column, separate the headers with \ t, and add a carriage return to the last column title.
Strexcelheader =   " Order No. \ t dealer \ t region \ t sub-category \ t goods \ t paid amount \ t order time \ t valid status \ t processing status \ t \ n " ;
// Write the obtained data to the HTTP output stream
Response. Write (strexcelheader );

// Process query result data row by row
Itdbhandle =   New Itdbhandle ();
Itdbhandle. querystring
= Viewstate [ " SQL " ]. Tostring ();
Sqldatareader Reader
= Itdbhandle. executedatareader ();
While (Reader. Read ())
{
Strexcelitems
=   String . Empty;
Strexcelitems
+ = Reader [ " Orderid " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " Dealername " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " City " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " Smallclassname " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " BrandName " ]. Tostring () + Reader [ " Model " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " Totalprice " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " Orderdate " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ "Is Valid " ]. Tostring () +   " \ T " ;
Strexcelitems
+ = Reader [ " Dealstate " ]. Tostring () + " \ N " ;
Response. Write (strexcelitems );
}
Reader. Close ();
Response. End ();
}


Here, I simply write the queried data in the form of Excel (of course, it can also be in other formats, such as XML). I have enough requirements for simplicity. Of course, I use it. net directly to the Excel file is also very simple, there are many such examples for reference.

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.