Recently, a small report system has nothing to do with its own functions. Finally, the customer requires a printing function. The so-called printing is to generate an Excel file for the report on the page.
I don't want to worry about complicated data logic again to create an Excel table with the same structure! Therefore, the following idea emerged: If I can obtain the report table on the page, can I create an Excel table as long as I analyze its structure?
After thinking about it, I thought this should be a way to go, so I began to find a way to achieve it. Finally, we found that webrequest and webresponse represent a Web request and a request-based response from the server. This response contains the data stream returned by the server, and the expected report table can be obtained from the data stream.
The idea of the entire Excel generation function is shown in:
Core of webrequest and webresponseCodeAs follows:
1 Webrequest WR = Webrequest. Create (psurl );
2 Httpwebrequest orequest = WR As Httpwebrequest;
3 If ( Null = Orequest) Return Null ;
4
5 Httpwebresponse oresponse; = (Httpwebresponse) orequest. getresponse ();
6 String Sresponsecontent = String . Empty;
7 If (Oresponse. statuscode = Httpstatuscode. OK)
8 {
9 Using (Streamreader SR = New Streamreader (oresponse. getresponsestream (), encoding. utf8 ))
10 {
11 Sresponsecontent = Sr. readtoend (); // Sresponsecontent, which stores the content of the data stream string returned by the server
12 Sr. Close ();
13 }
14 }
15 Oresponse. Close ();
In particular, many escape characters such as \ r \ t are left in the conversion process from the data to the string. We need to remove them.
1 Sresponsecontent = RegEx. Replace (sresponsecontent, " [\ N \ r \ t] " , "" );
This article will focus on the conversion process from table to excel cell.
Example code: excelgenerator.rar
RelatedArticle: Generate a WYSIWYG Excel report (2)-from HTML table to excel Cell