Java生成复杂word解决方案

01.导入依赖

<dependency>
   <groupId>cn.afterturn</groupId>
   <artifactId>easypoi-base</artifactId>
   <version>3.2.0</version>
</dependency>
<dependency>
   <groupId>cn.afterturn</groupId>
   <artifactId>easypoi-annotation</artifactId>
   <version>3.2.0</version>
</dependency>



<dependency>
   <groupId>cn.afterturn</groupId>
   <artifactId>easypoi-web</artifactId>
   <version>3.2.0</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi</artifactId>
   <version>3.15</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml-schemas</artifactId>
   <version>3.15</version>
</dependency>
<dependency>
   <groupId>org.apache.poi</groupId>
   <artifactId>poi-ooxml</artifactId>
   <version>3.15</version>
</dependency>

02. 工具类

/**
 * @Author: SwordsMan  133-8660-9751
 * @Date: 2020/4/10 14:30
 * @Describe:
 */
public class WordUtils {
    /**
     * 生成word
     *
     * @param path
     * @param params
     * @param tableList
     * @param fileName
     * @param response
     * @throws Exception
     */
    public void getWord(String path, Map<String, Object> params, List<List<String[]>> tableList, String fileName, HttpServletResponse response) throws Exception {

        URL url = WordUtils.class.getClassLoader().getResource(path);
        InputStream is = new FileInputStream(WordUtils.getResource(path));
        CustomXWPFDocument doc = new CustomXWPFDocument(is);
        this.replaceInPara(doc, params);//替换文本里面的变量
        for (int i = 0; i < tableList.size(); i++) {//2 个
            this.replaceInTable(doc, params, i, tableList.get(i));
        }
        //this.replaceInTable(doc, params, tableList); //替换表格里面的变量
        OutputStream os = response.getOutputStream();
        response.setHeader("Content-disposition", "attachment; filename=" + fileName);
        doc.write(os);
        this.close(os);
        this.close(is);
    }

    /**
     * 获取路径
     *
     * @param relativePath
     * @return
     * @throws IOException
     */
    public static String getResource(String relativePath) throws IOException {
        String filePath = System.getProperty("user.dir")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值