Implementation principle and code _java of generating HTML file with Java

Source: Internet
Author: User
The principle is similar to the servlet that generates JSP in Java. We can use PrintStream to output data to HTML files.
First, create a StringBuilder object and add an HTML statement to it by Append method. As shown in the following example:
Copy Code code as follows:

StringBuilder sb = new StringBuilder ();
Properties fileproperties = getProperties ("file");
Properties sqlproperties = getProperties ("SQL");
PrintStream PrintStream = new PrintStream (New FileOutputStream (
"report.html"));
Sb.append ("Sb.append ("Sb.append ("<title> daily Operation report </title>");
Sb.append ("<meta http-equiv=\" content-type\ "content=\" text/html; charset=utf-8\ "/>");
Sb.append ("<style type=\" text/css\ ">");
Sb.append ("Table{border-collapse:collapse;border-left:solid 1 #000000; Border-top:solid 1 #000000;p adding:5px} ");
Sb.append ("Th{border-right:solid 1 #000000; border-bottom:solid 1 #000000;}");
Sb.append ("Td{font:normal;border-right:solid 1 #000000; border-bottom:solid 1 #000000;}");
Sb.append ("</style>Sb.append ("<body bgcolor=\" #FFF8DC \ ">");
Sb.append ("<div align=\" Center\ ">");
Sb.append ("<br/>");
Sb.append ("<br/>");
list<map<string, object>> result1 = Getrpt (sqlproperties
. GetProperty ("SQL1"));
For (map.entry<string, object> m:result1.get (0). EntrySet ()) {
Sb.append (Fileproperties.getproperty ("file1"));
Sb.append (M.getvalue ());
}
Sb.append ("<br/><br/>");

Output, also very simple.
Copy Code code as follows:

Sb.append ("</div></body>Printstream.println (Sb.tostring ());

Upload a test HTML that I generated in Java, and I haven't called the online data yet. You can use the SQL statement, list to populate the table in practical application.

Here's an example I wrote
Copy Code code as follows:

Sb.append ("<table border=\" 1\ "><tr>");
For (map.entry<string, object> m:result.get (0). EntrySet ()) {
Sb.append ("<th>");
Sb.append (M.getkey ());
Sb.append ("</th>");
}
Sb.append ("</tr>");
for (int i = 0; i < result.size (); i++) {
Sb.append ("<tr>");
For (map.entry<string, object> m:result.get (i). EntrySet ()) {
Sb.append ("<td>");
Sb.append (M.getvalue ());
Sb.append ("</td>");
}
Sb.append ("</tr>");
}
Sb.append ("</table>");

That's the story. The episode is more exciting ~

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.