Home > Java > javaTutorial > Generate html files using java

Generate html files using java

高洛峰
Release: 2016-12-19 09:19:03
Original
1693 people have browsed it

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/>");
Copy after login

The output is also very simple.

sb.append("</div></body></html>");  
        printStream.println(sb.toString());
Copy after login

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.

Generate html files using java

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>");
Copy after login



For more articles related to using java to generate html files, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template