Java的XWPFTemplate word生成列表

Java的XWPFTemplate工具类导出word.docx的使用_xwpftemplate 语法_youmdt的博客-CSDN博客

如果是表格的列表参考上面这篇文章即可,比较复杂的列表遍历暂时还没找到方法,只能手动创建表格了

上面是模板,非常简单,以为我们是要自己创建表格

先看结果吧

自己写的表格基本就是这样,后面加上换行和宽度高度调节即可。

下面的代码是最基本的,所以也就没加高度换行等调节。有时候再补充上去。

下面的代码比较凌乱,还没开发完成,想整合成一个通用的接口,现在只做了日志的,把没有用的内容删掉即可。凑合着看。


    /**
     * 安全日志通用导出word
     */
    @Override
    public void exportWord(AqscSecurityLog aqscSecurityLog, HttpServletResponse response) throws IOException {

        String filePath = "";
        String filename = "";
        String params1 = aqscSecurityLog.getParams1();

        if (Objects.equals(params1, "1")) { //日报
            filePath = "/word/day.docx";
            filename = "安全日志.docx";
        } else if (Objects.equals(params1, "2")) { //周报
            filePath = "/word/day.docx";
            filename = "安全周报.docx";
        } else if (Objects.equals(params1, "3")) { //月报
            filePath = "/word/day.docx";
            filename = "安全月报.docx";
        } else if (Objects.equals(params1, "4")) { //季报
            filePath = "/word/day.docx";
            filename = "安全季报.docx";
        } else {
            throw new ServiceException("params1不等于1234,请联系管理员!");
        }

        InputStream inputStream = getServiceFile(filePath);
        XWPFTemplate document = XWPFTemplate.compile(inputStream);

        Map<String, Object> data = new HashMap<>();

        List<String> ids = aqscSecurityLog.getIds();

        List<AqscSecurityLog> logs = aqscSecurityLogMapper.selectAqscSecurityLogsByids(ids);
        //日志
        if (Objects.equals(params1, "1")) {
            processingDayData(logs, data, document);
        }

        document.render(data);
        exportWordAfter(document, response, filename);

    }

    //处理日志数据
    private void processingDayData(List<AqscSecurityLog> logs, Map<String, Object> data, XWPFTemplate document) {
        data.put("companyName", logs.get(0).getCompanyName());
        data.put("name", logs.get(0).getFillePerson());

        XWPFTable table2 = document.getXWPFDocument().createTable(1, 2);
        table2.getRow(0).getCell(0).setText("日 期");
        table2.getRow(0).getCell(1).setText("工作内容");
        table2.getRow(0).getCell(0).setWidth("30%");
        table2.getRow(0).getCell(1).setWidth("68%");

        logs.forEach(it -> {

            XWPFTable table = document.getXWPFDocument().createTable(3, 2);


            // 合并左边的三行
            for (int i = 0; i < 3; i++) {
                XWPFTableCell cell = table.getRow(i).getCell(0);
                if (i == 0) {
                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
                } else {
                    cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
                }
            }


            // 向表格中插入数据
            if (it.getWeather() != null) { //日期 (星期) (天气)
                table.getRow(0).getCell(0).setText(it.getStringTime() + "(" + it.getWeek() + ")" + it.getWeather());
            } else {
                table.getRow(0).getCell(0).setText(it.getStringTime() + "(" + it.getWeek() + ")");
            }
            table.getRow(0).getCell(1).setText(it.getContent());
            table.getRow(1).getCell(1).setText(it.getExperienceAndExperience());
            table.getRow(2).getCell(1).setText(it.getOtherRecords());


            table.getRow(0).getCell(0).setWidth("30%");
            table.getRow(0).getCell(1).setWidth("68%");


        });
    }


    public void exportWordAfter(XWPFTemplate workbook, HttpServletResponse response, String filename) {
        filename = URLEncodeUtil.encode(filename);
        response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.documentz");
        //test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
        response.setHeader("Content-Disposition", "attachment;filename=" + filename);
        response.setHeader("filename", filename);
        Assert.isTrue(!ObjectUtils.isEmpty(workbook), "导出遇到了问题,请联系管理员");
        try (ServletOutputStream out = response.getOutputStream()) {
            workbook.write(out);
            workbook.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

如果是返回到浏览器,前端也得配置一下,可以参考下面这篇文章配置

java-excel、word、zip返回前端-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
poi-tl是一个基于Apache POI的Java库,用于生成Word文档。它提供了简单易用的API,可以通过编程方式创建和编辑Word文档,包括添加文本、表格、图片、样式等内容。 使用poi-tl生成Word文档的基本步骤如下: 1. 导入poi-tl库:在项目中引入poi-tl的依赖库。 2. 创建Word文档对象:使用`XWPFTemplate`类创建一个空的Word文档对象。 3. 填充数据:通过模板引擎的方式,将数据填充到Word文档中。可以使用Freemarker或Velocity等模板引擎。 4. 导出文档:将填充好数据的Word文档导出为文件或输出流。 以下是一个使用poi-tl生成Word文档的示例代码: ```java // 导入依赖库 import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; // 创建一个空的Word文档对象 XWPFDocument document = new XWPFDocument(); // 添加段落 XWPFParagraph paragraph = document.createParagraph(); paragraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun run = paragraph.createRun(); run.setText("Hello, World!"); // 添加表格 XWPFTable table = document.createTable(3, 3); for (int row = 0; row < 3; row++) { XWPFTableRow tableRow = table.getRow(row); for (int col = 0; col < 3; col++) { XWPFTableCell tableCell = tableRow.getCell(col); tableCell.setText("Cell " + (row + 1) + "-" + (col + 1)); } } // 导出文档 FileOutputStream out = new FileOutputStream("output.docx"); document.write(out); out.close(); document.close(); ``` 这是一个简单的示例,你可以根据具体需求使用poi-tl的更多功能来生成复杂的Word文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值