Generate HTML file with Java to achieve the principle and code

  • 2020-04-01 02:07:49
  • OfStack

The principle is similar to a servlet that generates JSPS in Java. We can use printStream to output data to an HTML file.
Start by creating a StringBuilder object and adding HTML statements to it through the append method. As shown in the following example:
 
StringBuilder sb = new StringBuilder(); 
Properties fileProperties = getProperties("file"); 
Properties sqlProperties = getProperties("sql"); 
PrintStream printStream = new PrintStream(new FileOutputStream( 
"report.html")); 
sb.append("<html>"); 
sb.append("<head>"); 
sb.append("<title> Daily operating 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;padding: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></head>"); 
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/>"); 

The output is also very simple.
 
sb.append("</div></body></html>"); 
printStream.println(sb.toString()); 

Upload one more test HTML that I generated in Java and haven't called the online data yet. In practice, you can use SQL statement, list to fill the table.
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201308/201308151607521.gif? 201371516823 ">  
So let me give you an example that I wrote
 
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 next video is even better

Related articles: