码迷,mamicode.com
首页 > 编程语言 > 详细

java POI html生成word

时间:2020-03-04 15:11:17      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:size   new   lis   print   inpu   order   keyset   @param   out   

/**
 * html导出word
 * @param person 人员数据
 * @param wordfile word入径 
 * @param filtlist 过滤子弹
 * @param showCJ 是否导出成绩
 * @throws InvalidParameterException
 * @throws IOException
 */
public void createWord(BasicDBObject person,File wordfile,Map<String, String> filtlist,Boolean showCJ) throws IOException {
    StringBuffer html = new StringBuffer();
    for (String key : filtlist.keySet()) {
        if(filtlist.get(key).equals("4")||filtlist.get(key).equals("9"))
            person.put(key, null);
        else
            person.put(key, "***");
    }
    if(showCJ)
        html.append(CreateHtmlPrint(person,printContent2));
    
    html.append(CreateHtmlPrint(person,printContent1));
    
    POIFSFileSystem poifs = null;
    FileOutputStream ostream = null;
    ByteArrayInputStream bais = null;
    try {
        byte[] b = getHtml(html).toString().getBytes();
        bais = new ByteArrayInputStream(b);
        poifs = new POIFSFileSystem();
        DirectoryEntry directory = poifs.getRoot();
        directory.createDocument("WordDocument", bais);//WordDocument名称不允许修改
        ostream = new FileOutputStream(wordfile);
        poifs.writeFilesystem(ostream);//生成word文档
    } finally {
        if (ostream != null) ostream.close();
        if (bais != null) bais.close();
    }
}

public StringBuffer getHtml(Object in){
    StringBuffer html = new StringBuffer();
    html.append("<html>")
        .append("<head>")
        .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")
        .append("<style>")
        .append("BODY {PADDING-BOTTOM: 0px; LINE-HEIGHT: 1.5; FONT-STYLE: normal; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; COLOR: #333; FONT-SIZE: 13px; FONT-WEIGHT: normal; PADDING-TOP: 0px}")
        .append("table{border:1px solid #000;}")
        .append("table td{border:1px solid #000;}")
        .append("</style>")
        .append("</head>")
        .append("<body>");
    html.append(in);
    html.append("</body></html>");
    return html;
}

 

java POI html生成word

标签:size   new   lis   print   inpu   order   keyset   @param   out   

原文地址:https://www.cnblogs.com/BobXie85/p/12409058.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!