使用 poi 根据 word 模板生成 word 文件

本例子是一个 maven 项目,要引入 poi 的依赖片段如下:

<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poiartifactId>
    <version>3.13version>
dependency>


<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poi-scratchpadartifactId>
    <version>3.13version>
dependency>

注意:要想让 poi 操作 word ,须要引入 poi-scratchpad 这个模块。

本例子在开源中国代码托管的地址:
李威小朋友 / poi-word-template - 代码托管 - 开源中国社区
https://git.oschina.net/weimingge/poi-word-template

示例代码:

@Controller
@RequestMapping("/word")
public class DownLoadController {

    @RequestMapping(value = "/download",method = RequestMethod.POST)
    public void download(HttpServletRequest request,HttpServletResponse response){
        /**
         * 获取请求参数
         */
        String pzjg =  request.getParameter("pzjg"); // 篇章结构
        String gdnr =  request.getParameter("gdnr"); // 观点内容
        String jsyy =  request.getParameter("jsyy"); // 句式运用
        String chyf =  request.getParameter("chyf"); // 词汇语法
        String xzgf =  request.getParameter("xzgf"); // 写作规范

        // 获取应用的根路径
        String servletContextRealPath = request.getServletContext().getRealPath("");
        // 获取模板文件
        File templateFile = new File(servletContextRealPath + "/template/template1.doc");


        ByteArrayOutputStream ostream = null;
        try {
            FileInputStream in = new FileInputStream(templateFile);
            HWPFDocument hwpfDocument = new HWPFDocument(in);
            // 替换读取到的 word 模板内容的指定字段
            Map<String,String> params = new HashMap<>();
            params.put("$PZJG$",pzjg);
            params.put("$GDNR$",gdnr);
            params.put("$JSYY$",jsyy);
            params.put("$CHYF$",chyf);
            params.put("$XZGF$",xzgf);
            Range range = hwpfDocument.getRange();
            for(Map.Entry<String,String> entry:params.entrySet()){
                range.replaceText(entry.getKey(),entry.getValue());
            }
            // 输出 word 内容文件流,提供下载
            response.reset();
            response.setContentType("application/x-msdownload");

            // 随机生成一个文件名
            UUID randomUUID = UUID.randomUUID();
            String attachmentName = randomUUID.toString();
            response.addHeader("Content-Disposition", "attachment; filename=\""+ attachmentName + ".doc\"");
            ostream = new ByteArrayOutputStream();
            ServletOutputStream servletOS = response.getOutputStream();
            hwpfDocument.write(ostream);
            servletOS.write(ostream.toByteArray());
            servletOS.flush();
            servletOS.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

参考资料:

poi 根据模板调用静态数据
http://download.csdn.net/detail/lxlli/6413637
http://blog.csdn.net/classicbear/article/details/7549035
http://haohaoxuexi.iteye.com/blog/2031335
http://wenku.baidu.com/link?url=N65DYwHXIdl5NjZVgdJgzIgRu5VL2HyNFJ4kGCX0qlTYZTpp0U89817fQ-33898wNa7ms-pPShp2MigCceqxHZx6if2ddI68eMbp-qkBXBS
http://blog.csdn.net/longshengguoji/article/details/39433307
http://meigesir.iteye.com/blog/1539358
http://itindex.net/detail/45161-poi-excel-word

poi 动态调用数据

http://doc.okbase.net/53873039oycg/archive/110664.html

你可能感兴趣的:(poi)