DOC文档模板替换工具
本方法利用DOC文档书签的方式实现了对DOC模板文档的填充与获取填充数据的操作。
pom.xml
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <poi.version>3.17</poi.version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>${poi.version}</version> </dependency> </dependencies>
** * * @ClassName Word * @Description DOC文件文本替换 注意:当前仅支持 DOC文件, DOCX暂不支持 * @author Cheng.Wei * @date 2018年3月4日 下午6:26:11 * */ public class Word { /** * * 文件字符替换 注意:当前仅支持 DOC文件, DOCX暂不支持. * * @param source * 源路径 * @param target * 目标路径 * @param replaceBeans * 文本数据 * @throws IOException * 文件处理异常 */ public static void replaceText(String source, String target, ReplaceBean... replaceBeans) throws IOException { try (InputStream is = new FileInputStream(source); HWPFDocument doc = new HWPFDocument(is); OutputStream os = new FileOutputStream(target);) { Range range = doc.getRange(); for (ReplaceBean reb : replaceBeans) { range.replaceText(reb.getKey(), reb.getValue()); } doc.write(os); } } /** * 获取书签信息 注意:当前仅支持 DOC文件, DOCX暂不支持. * * @param source * 文件路径 * @return * @throws IOException * 书签读取异常 */ public static List<BookMark> getBookmarkers(String source) throws IOException { try (InputStream inputStream = new FileInputStream(source); HWPFDocument hwpfDocument = new HWPFDocument(inputStream);) { Bookmarks bookmarks = hwpfDocument.getBookmarks(); Bookmark bookmark; BookMark bookm; List<BookMark> list = new ArrayList<>(); String text = hwpfDocument.getDocumentText(); for (int i = 0, length = bookmarks.getBookmarksCount(); i < length; i++) { bookmark = bookmarks.getBookmark(i); bookm = new BookMark(); bookm.setName(bookmark.getName()); bookm.setContent(text.substring(bookmark.getStart(), bookmark.getEnd())); list.add(bookm); } return list; } } }
演示:
** * @ClassName DocTest * @Description doc解析测试 * @author Cheng.Wei * @date 2018年3月4日 下午6:27:10 * */ public class DocTest { /** * 获取DOC中的书签 * * @throws IOException */ @Test public void bookmark() throws IOException { List<BookMark> bookmarks = Word.getBookmarkers("d://123.doc"); for (BookMark bookMark : bookmarks) { System.out.println(bookMark.toString()); } } @Test public void replaceText() throws IOException { // 句子内容填充 ReplaceBean replaceBeanA = new ReplaceBean(); replaceBeanA.setKey("{poetry}"); replaceBeanA.setValue("秋水共长天一色"); // 表格书签内容填充 ReplaceBean replaceBeanB = new ReplaceBean(); replaceBeanB.setKey("{name}"); replaceBeanB.setValue("张三"); ReplaceBean replaceBeanC = new ReplaceBean(); replaceBeanC.setKey("{sex}"); replaceBeanC.setValue("男"); ReplaceBean[] replaceBeans = new ReplaceBean[] {replaceBeanA, replaceBeanB, replaceBeanC}; // 执行填充 Word.replaceText("D://123.doc", "D://666.doc", replaceBeans); } }
获取书签:
填充:
/** * @ClassName BookMark * @Description 书签信息 * @author Cheng.Wei * @date 2018年1月11日 下午5:56:31 * */ public class BookMark { /** * 书签 */ private String name; /** * 书签内容 */ private String content; /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @return the content */ public String getContent() { return content; } /** * @param content * the content to set */ public void setContent(String content) { this.content = content; } @Override public String toString() { return "BookMark [name=" + name + ", content=" + content + "]"; } }
/** * @ClassName ReplaceBean * @Description 数据替换信息 * @author Cheng.Wei * @date 2018年1月11日 下午1:27:07 * */ public class ReplaceBean { /** * 属性名 */ private String key; /** * 属性值 */ private String value; /** * */ public ReplaceBean() { super(); } /** * @param key * @param value */ public ReplaceBean(String key, String value) { super(); this.key = key; this.value = value; } /** * @return the key */ public String getKey() { return key; } /** * @param key * the key to set */ public void setKey(String key) { this.key = key; } /** * @return the value */ public String getValue() { return value; } /** * @param value * the value to set */ public void setValue(String value) { this.value = value; } @Override public String toString() { return "ReplaceBean [key=" + key + ", value=" + value + "]"; } }