文章目录 一、新建Word模板二、导入依赖三、编写代码1、替换文字2、插入图片 提示:以下是本篇文章正文内容,下面案例可供参考 一、新建Word模板 在你需要动态生成的内容使用{fi
提示:以下是本篇文章正文内容,下面案例可供参考
在你需要动态生成的内容使用{field}作为占位符,后面代码扫描到就将占位符换成自己的动态内容。
<dependency> <groupId>org.apache.poigroupId> <artifactId>poiartifactId> <version>4.1.2version> dependency> <dependency> <groupId>org.apache.poigroupId> <artifactId>poi-ooxmlartifactId> <version>4.1.2version> dependency> <dependency> <groupId>commons-iogroupId> <artifactId>commons-ioartifactId> <version>2.11.0version> dependency>
代码如下(示例):
public static void main(String[] args) { try { String templatePath = "C:\\Users\\Administrator\\Desktop\\test3.docx"; String outputPath = "C:\\Users\\Administrator\\Desktop\\output.docx"; InputStream inputStream = new FileInputStream(templatePath); XWPFDocument document = new XWPFDocument(inputStream); Map<String, String> HashMap = new HashMap<>(); hashMap.put("name", "JUVENILESS"); hashMap.put("gender", "男"); hashMap.put("schoolNumber", "13123132"); hashMap.put("qq", "123123"); hashMap.put("nativePlace", "测试"); hashMap.put("profession", "测试啊"); hashMap.put("birth", "2025-10"); hashMap.put("dORM", "I123"); hashMap.put("cellNumber", "12345678911"); hashMap.put("introduction", "简单的自我介绍"); hashMap.put("futureOutlook", "简单的对未来发展"); hashMap.put("award", "拿过的奖项拿过的奖项"); hashMap.put("department","部门"); hashMap.put("year","年份"); // 替换占位符 replacePlaceholdersInDocument(document, hashMap); // 插入图片 insertPictureInTableCell(document, "{picture}", picturePath); // 保存生成的文档 FileOutputStream outputStream = new FileOutputStream(outputPath); document.write(outputStream); outputStream.close(); System.out.println("生成的文档已保存。"); } catch (Exception e) { e.printStackTrace(); } } private static void replacePlaceholdersInDocument(XWPFDocument document, Map<String, String> hashMap) { for (XWPFParagraph paragraph : document.getParagraphs()) { replaceParagraPHPlaceholders(paragraph, hashMap); } for (XWPFTable table : document.getTables()) { for (XWPFTableRow row : table.getRows()) { for (XWPFTableCell cell : row.getTableCells()) { for (XWPFParagraph paragraph : cell.getParagraphs()) { replaceParagraphplaceholders(paragraph, hashMap); } } } } }
直接运行即可。运行结果图为:
这里我是查找表格的单元格里的占位符。
代码如下(示例):
static String picturePath = "C:\\Users\\Administrator\\Desktop\\picture.jpg"; public static void main(String[] args) { try { String templatePath = "C:\\Users\\Administrator\\Desktop\\test4.docx"; String outputPath = "C:\\Users\\Administrator\\Desktop\\output1.docx"; InputStream inputStream = new FileInputStream(templatePath); XWPFDocument document = new XWPFDocument(inputStream); // 插入图片 insertPictureInTableCell(document, "{picture}", picturePath); // 保存生成的文档 FileOutputStream outputStream = new FileOutputStream(outputPath); document.write(outputStream); outputStream.close(); System.out.println("生成的文档已保存。"); } catch (Exception e) { e.printStackTrace(); } } private static void insertPictureInTableCell(XWPFDocument document, String placeholder, String picturePath) throws Exception { List<XWPFTable> tables = new ArrayList<>(document.getTables()); for (XWPFTable table : tables) { List<XWPFTableRow> rows = new ArrayList<>(table.getRows()); for (XWPFTableRow row : rows) { List<XWPFTableCell> cells = new ArrayList<>(row.getTableCells()); for (XWPFTableCell cell : cells) { List<XWPFParagraph> paragraphs = new ArrayList<>(cell.getParagraphs()); for (XWPFParagraph paragraph : paragraphs) { List<XWPFRun> runs = new ArrayList<>(paragraph.getRuns()); for (XWPFRun run : runs) {String text = run.getText(0);if (text != null && text.contains(placeholder)) { int index = text.indexOf(placeholder); if (index >= 0) { // 移除占位符 run.setText(text.replace(placeholder, ""), 0); // 在当前段落中插入图片 XWPFRun pictureRun = paragraph.createRun(); try (FileInputStream pictureInputStream = new FileInputStream(picturePath)) { pictureRun.addPicture(pictureInputStream, Document.PICTURE_TYPE_JPEG, "picture.jpg", Units.toEMU(200), Units.toEMU(200)); } }} } } } } } }
生成的结果如下:
这里我发现图片的尺寸没法保证跟单元格大小一致,也尝试过很多种方法都不太行。哪位大佬尝试尝试哈哈哈哈
来源地址:https://blog.csdn.net/a12789sd/article/details/131451986
--结束END--
本文标题: java根据自定义的word模板生成文档
本文链接: https://lsjlt.com/news/420772.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-04-01
2024-04-03
2024-04-03
2024-01-21
2024-01-21
2024-01-21
2024-01-21
2023-12-23
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0