java word文档导出统计功能

  1. 上代码
@GetMapping("/word")
    public void generateWord(HttpServletResponse response) throws IOException, XDocReportException, FileNotFoundException {
        //获取Word模板,模板存放路径在项目的resources目录下
        InputStream ins = this.getClass().getResourceAsStream("/templates/operatingStatistics.docx");
        //注册xdocreport实例并加载FreeMarker模板引擎
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(ins,
            TemplateEngineKind.Freemarker);
        //创建xdocreport上下文对象
        IContext context = report.createContext();

        //创建要替换的文本变量
        context.put("unitName", "恩施州保障中心");

        List<UnitStatistics> goodsList = new ArrayList<UnitStatistics>();
        UnitStatistics goods1 = new UnitStatistics();
        goods1.setInitiatorUnitCodeName("单位111");
        goods1.setDateCount(11);
        goods1.setMileages(675512L);
        goods1.setFrequency("589");
        UnitStatistics goods2 = new UnitStatistics();
        goods1.setInitiatorUnitCodeName("单位222");
        goods1.setDateCount(12);
        goods1.setMileages(675512L);
        goods1.setFrequency("9");
        UnitStatistics goods3 = new UnitStatistics();
        goods1.setInitiatorUnitCodeName("单位333");
        goods1.setDateCount(13);
        goods1.setMileages(76312L);
        goods1.setFrequency("12");
        UnitStatistics goods4 = new UnitStatistics();
        goods1.setInitiatorUnitCodeName("单位444");
        goods1.setDateCount(14);
        goods1.setMileages(679812L);
        goods1.setFrequency("36");
        goodsList.add(goods1);
        goodsList.add(goods2);
        goodsList.add(goods3);
        goodsList.add(goods4);
        context.put("goods", goodsList);

        //创建字段元数据
        FieldsMetadata fm = report.createFieldsMetadata();
        //Word模板中的表格数据对应的集合类型
        fm.load("goods", UnitStatistics.class, true);

        //输出到本地目录
//        FileOutputStream out = new FileOutputStream(new File("D://运行统计表demo.docx"));
//        report.process(context, out);
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/msword");
        String fileName = "统计分析报表.docx";
        response.setHeader("Content-Disposition", "attachment;filename="
            .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
        report.process(context, response.getOutputStream());
    }


<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>4.1.1</version>
</dependency>
<dependency>
    <groupId>org.jxls</groupId>
    <artifactId>jxls</artifactId>
    <version>2.6.0</version>
    <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.jxls</groupId>
    <artifactId>jxls-poi</artifactId>
    <version>1.2.0</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.core</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.document.docx</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>fr.opensagres.xdocreport</groupId>
    <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.23</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>
  1. 看模板
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值