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博客

相关推荐
热爱生活的五柒9 分钟前
word中如何一键修改英文字母数字为新罗马字体Times New Roman
word·西文·times new roman
伟贤AI之路7 小时前
Markdown写彩色文字+公式,MD2WD直接转Word,保留所有格式
word·markdown
bu_shuo8 小时前
Mathtype的公式在word中显示不全的问题
word·学位论文·mathtype·论文格式
E_ICEBLUE1 天前
在 Python 中对比 Word 文档:自动生成修订报告
python·word
大模型RAG和Agent技术实践1 天前
破译Word文档的“语义黑盒”:企业级DOCX RAG架构演进与全链路实战(完整源代码)
人工智能·架构·大模型·word·智能问答·rag
superior tigre1 天前
word参考文献交叉引用的方法(包括批量把交叉引用改为上标、保留跳转功能到pdf)
word
我喜欢就喜欢1 天前
Word 模板匹配与样式同步技术详解
开发语言·c++·qt·word·模板匹配
拾穗哥2 天前
wps/word行距不正常调整
word·wps
重生之光头强下海当程序猿2 天前
调整word中的序号格式(缩进,起始值,序号与文字的间距等
前端·css·word
Eiceblue3 天前
C# 中如何设置 Word 文档页面?(页面大小、边距、方向自动化控制)
c#·自动化·word·visual studio