The principle is similar to using Java to generate jsp servlet. We can use printStream to output data to html files.
First create a StringBuilder object and add 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>每日运营报表</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 another test html that I generated using java, but the online data has not been called yet. In practical applications, you can use sql statements and lists to fill tables.
Here is an example 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>");
For more articles related to using java to generate html files, please pay attention to the PHP Chinese website!