java生成word报告

使用easypoi生成word报告

1、依赖
<!--下载word demo-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>4.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jcommon</artifactId>
            <version>1.0.24</version>
        </dependency>
        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.5.0</version>
        </dependency>
2、创建模板 demo.docx

使用word,新建一个demo.docx,内容粘贴如下:由于不能导入word格式,所以,我截了个图,放在下面了。

3、编写工具类,用于生成word文档
package com.example.demo.wordTest.GenerateWordUtils;

/**
 * @author fj
 * @date 2022-09-14 08:38
 * @description
 */
import cn.afterturn.easypoi.word.WordExportUtil;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.util.Assert;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

/**
 * @author: cfj
 * @date:2022-09-14
 */
public class wordutil {
    /**
     * 导出word
     * <p>第一步生成替换后的word文件,只支持docx</p>
     * <p>第二步下载生成的文件</p>
     * <p>第三步删除生成的临时文件</p>
     * 模版变量中变量格式:{{foo}}
     *
     * @param templatePath word模板地址
     * @param temDir       生成临时文件存放地址
     * @param fileName     文件名
     * @param params       替换的参数
     */
    public static void exportWord(String templatePath, String temDir, String fileName, Map<String, Object> params) {
        Assert.notNull(templatePath, "模板路径不能为空");
        Assert.notNull(temDir, "临时文件路径不能为空");
        Assert.notNull(fileName, "导出文件名不能为空");
        Assert.isTrue(fileName.endsWith(".docx"), "word导出请使用docx格式");
        if (!temDir.endsWith("/")) {
            temDir = temDir + File.separator;
        }
        File dir = new File(temDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            XWPFDocument doc = WordExportUtil.exportWord07(templatePath, params);
            String tmpPath = temDir + fileName;
            FileOutputStream fos = new FileOutputStream(tmpPath);
            doc.write(fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

4、编写测试代码

package com.example.demo.wordTest.GenerateWordUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * @author fj
 * @date 2022-09-14 09:15
 * @description
 */
public class wordDemo {
    public static void main(String[] args) {
        HashMap<String, Object> map = new HashMap<>();

        //模拟其它普通数据
        map.put("enName", "公司人员");
        map.put("username", "张三");
        map.put("date", "2019-10-10");
        map.put("desc", "测试");
        map.put("boo", "这是一段很长的描述,这是一段很长的说明阿附件三;阿济格你;是否是将连接 \" +\n" +
                "                \"罚单否卡萨丁平万科蓝山,发送副经理。到家了开讲啦,的奖金额,第三方就拉萨尔,第三方就老魏到家了就饿了干的");
        map.put("bool1","这是测试合并单元格");
        //模拟表格数据
        ArrayList<HashMap<String, String>> list = new ArrayList<>();
        HashMap<String, String> temp = new HashMap<>();
        temp.put("sn","1");
        temp.put("name","设备1");
        temp.put("age","设备问题1");
        temp.put("exception", "有异常");
        temp.put("qu", "这个设备有问题,会造成断电");
        list.add(temp);
        temp = new HashMap<>();
        temp.put("sn","2");
        temp.put("name","设备2");
        temp.put("age","设备问题2");
        temp.put("exception", "无");
        temp.put("qu", "无");
        list.add(temp);
        temp = new HashMap<>();
        temp.put("sn","3");
        temp.put("name","设备3");
        temp.put("age","设备问题3");
        temp.put("exception", "有");
        temp.put("qu", "无危害");
        list.add(temp);
        temp = new HashMap<>();
        temp.put("sn","4");
        temp.put("name","设备4");
        temp.put("age","设备问题4");
        temp.put("exception", "有");
        temp.put("qu", "无危害");
        list.add(temp);
        map.put("personlist",list);
        //word模板绝对路径、word生成路径、word生成的文件名称、数据源参数
        wordutil.exportWord("E:\\cfj\\ware\\demo.docx", "E:\\cfj\\ware\\", "生成文件.docx", map);
    }
}

5、直接执行main方法

打开 E:\cfj\ware\生成文件.docx,成功生成"生成文件.docx文档"

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值