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.