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

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

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.13</version>
</dependency>

<!-- 支持Word文档的操作 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>3.13</version>
</dependency>
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

注意:要想让 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();
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

参考资料:

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

转载自http://blog.csdn.net/lw_power/article/details/49890921

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值