Word模板填充-基于Poi-tl动态填充多张图片(表格样式)

1.生成效果

在这里插入图片描述

word模板

在这里插入图片描述

核心代码

           //填充数据
           Map<String, Object> dateMap = new HashMap<>();
            //新增表格
            TableRenderData tableRenderData = Tables.ofA4Width().cellMargin(0.19f, 0.19f, 0.19f, 0.19f).center().create();
            //第一行图片
            {
                //单元格
                CellRenderData cell1 = new CellRenderData();
                CellRenderData cell2 = new CellRenderData();
                {
                    String image = "图片1.jpg";
                    byte[] imageByte = null;
                    PictureRenderData pic1 = new PictureRenderData(260, 200, getType(image), imageByte);
                    ParagraphRenderData graph1 = new ParagraphRenderData();
                    graph1.addPicture(pic1);
                    cell1.addParagraph(graph1);
                }
                {
                    String image = "图片2.jpg";
                    byte[] imageByte = null;
                    PictureRenderData pic1 = new PictureRenderData(260, 200, getType(image), imageByte);
                    ParagraphRenderData graph1 = new ParagraphRenderData();
                    graph1.addPicture(pic2);
                    cell2.addParagraph(graph1);
                }
                tableRenderData.addRow(Rows.create(cell1, cell2));
            }
            //第二行图片名称
            {
                //单元格
                String cell1 = "图片1";
                String cell2 = "图片2";
                tableRenderData.addRow(Rows.of(cell1, cell2).center().textFontSize(9).rowAtleastHeight(0.6).create());
            }
            dateMap.put("imageTables", tableRenderData);
           //去生成....
   
    /**
     * 获取图片类型
     *
     * @param path
     * @return
     */
    PictureType getType(String path) {
        //判断图片的格式
        PictureType format;
        if (path.endsWith(".emf")) {
            format = PictureType.EMF;
        } else if (path.endsWith(".wmf")) {
            format = PictureType.WMF;
        } else if (path.endsWith(".pict")) {
            format = PictureType.PICT;
        } else if (path.endsWith(".jpeg") || path.endsWith(".jpg")) {
            format = PictureType.JPEG;
        } else if (path.endsWith(".png")) {
            format = PictureType.PNG;
        } else if (path.endsWith(".dib")) {
            format = PictureType.DIB;
        } else if (path.endsWith(".gif")) {
            format = PictureType.GIF;
        } else if (path.endsWith(".tiff")) {
            format = PictureType.TIFF;
        } else if (path.endsWith(".eps")) {
            format = PictureType.EPS;
        } else if (path.endsWith(".bmp")) {
            format = PictureType.BMP;
        } else if (path.endsWith(".wpg")) {
            format = PictureType.WPG;
        } else {
            throw new BizException("不支持的图片类型Unsupported picture: " + path + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
        }
        return format;
    }
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当使用Poi-tl库根据Word模板填充内容生成Word文档时,可以使用Poi-tl提供的模板语法来处理空值。以下是一个示例代码: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.ParagraphAlignment; import org.apache.poi.xwpf.usermodel.TextAlignment; import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter; import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.Document; import org.apache.poi.xwpf.usermodel.HeaderFooterType; import org.apache.poi.xwpf.usermodel.IBodyElement; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableRow; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.Map; public class WordTemplateFiller { public static void main(String[] args) { try { // 加载Word模板文件 FileInputStream templateStream = new FileInputStream("template.docx"); XWPFDocument document = new XWPFDocument(templateStream); // 填充内容 Map<String, Object> placeholders = new HashMap<>(); placeholders.put("name", "John Doe"); placeholders.put("age", "30"); placeholders.put("address", ""); replacePlaceholders(document, placeholders); // 保存填充后的文档 FileOutputStream outputStream = new FileOutputStream("filled_template.docx"); document.write(outputStream); outputStream.close(); System.out.println("Word文档生成成功!"); } catch (IOException e) { e.printStackTrace(); } } private static void replacePlaceholders(XWPFDocument document, Map<String, Object> placeholders) { for (XWPFParagraph paragraph : document.getParagraphs()) { for (XWPFRun run : paragraph.getRuns()) { String text = run.getText(0); if (text != null) { for (Map.Entry<String, Object> entry : placeholders.entrySet()) { String placeholder = "${" + entry.getKey() + "}"; if (text.contains(placeholder)) { Object value = entry.getValue(); if (value != null && !value.toString().isEmpty()) { text = text.replace(placeholder, value.toString()); } else { text = text.replace(placeholder, ""); // 替换为空字符串 } run.setText(text, 0); } } } } } } } ``` 在上述代码中,我们首先加载Word模板文件,然后定义了一个`placeholders`的映射,其中包含了要替换的占位符和对应的值。接下来,我们调用`replacePlaceholders`方法来替换文档中的占位符。 在`replacePlaceholders`方法中,我们遍历文档中的每个段落和文本运行,通过检查文本内容中是否包含占位符来确定是否需要替换。如果找到了匹配的占位符,则根据占位符对应的值来进行替换。如果值不为空且非空字符串,则将占位符替换为对应的值;如果值为空或空字符串,则将占位符替换为空字符串。 请注意,上述代码中使用的占位符格式为`${placeholder}`,你可以根据实际情况修改为其他格式。 以上是一个基本示例,你可以根据自己的需求进行修改和扩展。同时,需要确保在项目中添加了Poi-tl的依赖库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值