From db7e077ea8f2d995e922bc11b77dc149592a7455 Mon Sep 17 00:00:00 2001 From: yupeng <roc__yu@163.com> Date: 星期三, 09 四月 2025 12:35:49 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' into xizang-changyun --- ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/WordTemplateProcessor.java | 174 +++++++++++++++++++++++++++++----------------------------- 1 files changed, 87 insertions(+), 87 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/WordTemplateProcessor.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/WordTemplateProcessor.java index 6b32fe3..f847008 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/WordTemplateProcessor.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/tool/WordTemplateProcessor.java @@ -1,87 +1,87 @@ -package com.ruoyi.web.controller.tool; - -import org.apache.poi.xwpf.usermodel.*; -import java.io.*; -import java.util.*; - -public class WordTemplateProcessor { - - public static void fillTemplate(String templatePath, String outputPath,Map<String, String> dataMap) { - try { - // 读取模板文件 - FileInputStream fis = new FileInputStream(templatePath); - XWPFDocument document = new XWPFDocument(fis); - - // 替换段落中的标记 - for (XWPFParagraph paragraph : document.getParagraphs()) { - replaceParagraph(paragraph, dataMap); - } - - // 替换表格中的标记 - for (XWPFTable table : document.getTables()) { - for (XWPFTableRow row : table.getRows()) { - for (XWPFTableCell cell : row.getTableCells()) { - for (XWPFParagraph paragraph : cell.getParagraphs()) { - replaceParagraph(paragraph, dataMap); - } - } - } - } - - // 保存文件 - FileOutputStream fos = new FileOutputStream(outputPath); - document.write(fos); - - // 关闭资源 - fos.close(); - fis.close(); - document.close(); - - System.out.println("模板填充完成!文件保存在: " + outputPath); - - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static void replaceParagraph(XWPFParagraph paragraph, Map<String, String> dataMap) { - String paragraphText = paragraph.getText(); - for (Map.Entry<String, String> entry : dataMap.entrySet()) { - if (paragraphText.contains(entry.getKey())) { - List<XWPFRun> runs = paragraph.getRuns(); - TextSegment found = paragraph.searchText(entry.getKey(), new PositionInParagraph()); - if (found != null) { - // 替换文本 - int beginRun = found.getBeginRun(); - int endRun = found.getEndRun(); - - if (beginRun >= 0 && endRun >= 0) { - // 删除原有runs - for (int runPos = beginRun; runPos <= endRun; runPos++) { - paragraph.removeRun(runPos); - } - // 创建新run - XWPFRun newRun = paragraph.insertNewRun(beginRun); - newRun.setText(entry.getValue()); - // 复制原有格式 - if (runs.size() > 0 && runs.get(0) != null) { - XWPFRun styleRun = runs.get(0); - newRun.setFontFamily(styleRun.getFontFamily()); - newRun.setFontSize(styleRun.getFontSize()); - newRun.setBold(styleRun.isBold()); - newRun.setItalic(styleRun.isItalic()); - } - } - } - } - } - } - - public static void main(String[] args) { - - String templatePath = "/path/to/template.docx"; - String outputPath = "/path/to/output.docx"; - -// fillTemplate(templatePath, outputPath, user); - } -} \ No newline at end of file +//package com.ruoyi.web.controller.tool; +// +//import org.apache.poi.xwpf.usermodel.*; +//import java.io.*; +//import java.util.*; +// +//public class WordTemplateProcessor { +// +// public static void fillTemplate(String templatePath, String outputPath,Map<String, String> dataMap) { +// try { +// // 读取模板文件 +// FileInputStream fis = new FileInputStream(templatePath); +// XWPFDocument document = new XWPFDocument(fis); +// +// // 替换段落中的标记 +// for (XWPFParagraph paragraph : document.getParagraphs()) { +// replaceParagraph(paragraph, dataMap); +// } +// +// // 替换表格中的标记 +// for (XWPFTable table : document.getTables()) { +// for (XWPFTableRow row : table.getRows()) { +// for (XWPFTableCell cell : row.getTableCells()) { +// for (XWPFParagraph paragraph : cell.getParagraphs()) { +// replaceParagraph(paragraph, dataMap); +// } +// } +// } +// } +// +// // 保存文件 +// FileOutputStream fos = new FileOutputStream(outputPath); +// document.write(fos); +// +// // 关闭资源 +// fos.close(); +// fis.close(); +// document.close(); +// +// System.out.println("模板填充完成!文件保存在: " + outputPath); +// +// } catch (Exception e) { +// e.printStackTrace(); +// } +// } +// +// private static void replaceParagraph(XWPFParagraph paragraph, Map<String, String> dataMap) { +// String paragraphText = paragraph.getText(); +// for (Map.Entry<String, String> entry : dataMap.entrySet()) { +// if (paragraphText.contains(entry.getKey())) { +// List<XWPFRun> runs = paragraph.getRuns(); +// TextSegment found = paragraph.searchText(entry.getKey(), new PositionInParagraph()); +// if (found != null) { +// // 替换文本 +// int beginRun = found.getBeginRun(); +// int endRun = found.getEndRun(); +// +// if (beginRun >= 0 && endRun >= 0) { +// // 删除原有runs +// for (int runPos = beginRun; runPos <= endRun; runPos++) { +// paragraph.removeRun(runPos); +// } +// // 创建新run +// XWPFRun newRun = paragraph.insertNewRun(beginRun); +// newRun.setText(entry.getValue()); +// // 复制原有格式 +// if (runs.size() > 0 && runs.get(0) != null) { +// XWPFRun styleRun = runs.get(0); +// newRun.setFontFamily(styleRun.getFontFamily()); +// newRun.setFontSize(styleRun.getFontSize()); +// newRun.setBold(styleRun.isBold()); +// newRun.setItalic(styleRun.isItalic()); +// } +// } +// } +// } +// } +// } +// +// public static void main(String[] args) { +// +// String templatePath = "/path/to/template.docx"; +// String outputPath = "/path/to/output.docx"; +// +//// fillTemplate(templatePath, outputPath, user); +// } +//} \ No newline at end of file -- Gitblit v1.7.1