目录

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

本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
pen-ai9 小时前
【NLP】 5. Word Analogy Task(词类比任务)与 Intrinsic Metric(内在度量)
人工智能·自然语言处理·word
荷包蛋大王iovo15 小时前
Word 小黑第27套
word
菜鸟一皓15 小时前
告别XML模板的繁琐!Word文档导出,easy!
java·word
黎栗栗16 小时前
word排版:段内公式由于固定行间距显露不出的问题
word
黄鹂绿柳2 天前
今日所学——Word批量给图片插入题注,设置题注起始编号
word
荷包蛋大王iovo2 天前
Word 小黑第19套
word
东方佑3 天前
Python中将Markdown文件转换为Word
python·c#·word
伊织code3 天前
python-docx - 读写更新 .docx 文件(Microsoft Word 2007+ )
python·microsoft·word·docx·python-docx·openxml
典龙3303 天前
python语言写的一款pdf转word、word转pdf的免费工具
pdf·word
YEAH!启动!3 天前
android 调用wps打开文档并感知保存事件
android·编辑器·word·excel·wps·ppt