Generate a WYSIWYG Excel report (1) -- Obtain the HTML table structure

Source: Internet
Author: User

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

 

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.