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

相关推荐
GEEKVIP1 天前
如何在 Windows 10 上恢复未保存/删除的 Word 文档
macos·ios·智能手机·电脑·word·笔记本电脑·iphone
不写八个2 天前
Python办公自动化教程(005):Word添加段落
开发语言·python·word
GEEKVIP2 天前
如何修复变砖的手机并恢复丢失的数据
macos·ios·智能手机·word·手机·笔记本电脑·iphone
ChinaDragonDreamer4 天前
Word:表格公式计算
word
ziyue75754 天前
java将word转pdf
java·pdf·word
fxybg20224 天前
探索高效免费的PDF转Word工具,开启便捷办公之旅
人工智能·自然语言处理·pdf·word·机器翻译
几度热忱4 天前
【word脚注】双栏设置word脚注,脚注仅位于左栏,右栏不留白
word·脚注
奇客软件5 天前
如何在 macOS(MacBook Pro、Air 和 iMac)上恢复未保存的 Word 文档
android·windows·macos·智能手机·word·笔记本电脑·iphone
GEEKVIP5 天前
如何在 Windows PC 或笔记本电脑上恢复未保存的 Word 文档
windows·macos·ios·智能手机·word·cocoa·iphone
三木一立5 天前
Word导出样式模板,应用到其他所有word
word