使用POI读写word docx文件
来源:互联网 发布:药品网络销售许可证 编辑:程序博客网 时间:2024/05/04 17:49
docx文件作为模板
当然,我们也可以像写doc文件那样,先以一个docx文件作为模板,然后建立基于该docx文件的XWPFDocument对象,再把里面一些变化的信息在运行时进行替换,之后将XWPFDocument进行输出就可以了。所不同的是XWPFDocument中没有像HWPFDocument中那样的Range可以用来直接替换内容。而且底层的XWPFParagraph和XWPFRun也不支持直接将文本进行替换。倒是XWPFRun提供了一个设置文本的方法,不过新的文本不会替换旧的文本,而是会追加到原来的文本之后。现在的一个做法是先找出含有需要替换的变量的XWPFRun,然后将其移除,之后在原来的位置新增一个XWPFRun,其对应的文本是替换变量之后的文本。不过你设置的那个的变量的位置不一定就在一个XWPFRun里面,它有可能会被拆分到两个甚至更多的XWPFRun中,所以不是很有必要的话还是不推荐使用这种方式。
假设我们有一个docx文件,其内容是这样的:
之后我们以该文件作为模板,利用相关数据把里面的变量进行替换,然后把替换后的文档输出到另一个docx文件中。具体做法如下:
Java代码
public class XwpfTest { /** * 用一个docx文档作为模板,然后替换其中的内容,再写入目标文档中。 * @throws Exception */ @Test public void testTemplateWrite() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put("reportDate", "2014-02-28"); params.put("appleAmt", "100.00"); params.put("bananaAmt", "200.00"); params.put("totalAmt", "300.00"); String filePath = "D:\\word\\template.docx"; InputStream is = new FileInputStream(filePath); XWPFDocument doc = new XWPFDocument(is); //替换段落里面的变量 this.replaceInPara(doc, params); //替换表格里面的变量 this.replaceInTable(doc, params); OutputStream os = new FileOutputStream("D:\\word\\write.docx"); doc.write(os); this.close(os); this.close(is); } /** * 替换段落里面的变量 * @param doc 要替换的文档 * @param params 参数 */ private void replaceInPara(XWPFDocument doc, Map<String, Object> params) { Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator(); XWPFParagraph para; while (iterator.hasNext()) { para = iterator.next(); this.replaceInPara(para, params); } } /** * 替换段落里面的变量 * @param para 要替换的段落 * @param params 参数 */ private void replaceInPara(XWPFParagraph para, Map<String, Object> params) { List<XWPFRun> runs; Matcher matcher; if (this.matcher(para.getParagraphText()).find()) { runs = para.getRuns(); for (int i=0; i<runs.size(); i++) { XWPFRun run = runs.get(i); String runText = run.toString(); matcher = this.matcher(runText); if (matcher.find()) { while ((matcher = this.matcher(runText)).find()) { runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1)))); } //直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面, //所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。 para.removeRun(i); para.insertNewRun(i).setText(runText); } } } } /** * 替换表格里面的变量 * @param doc 要替换的文档 * @param params 参数 */ private void replaceInTable(XWPFDocument doc, Map<String, Object> params) { Iterator<XWPFTable> iterator = doc.getTablesIterator(); XWPFTable table; List<XWPFTableRow> rows; List<XWPFTableCell> cells; List<XWPFParagraph> paras; while (iterator.hasNext()) { table = iterator.next(); rows = table.getRows(); for (XWPFTableRow row : rows) { cells = row.getTableCells(); for (XWPFTableCell cell : cells) { paras = cell.getParagraphs(); for (XWPFParagraph para : paras) { this.replaceInPara(para, params); } } } } } /** * 正则匹配字符串 * @param str * @return */ private Matcher matcher(String str) { Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(str); return matcher; } /** * 关闭输入流 * @param is */ private void close(InputStream is) { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 关闭输出流 * @param os */ private void close(OutputStream os) { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } }
经过上面的代码所示的过程处理后,我们替换变量后新输出来的docx文件的内容是这样的:
点击打开转载链接
0 0
- 使用POI读写word docx文件
- 使用POI读写word docx文件
- 使用POI读写word docx文件
- 使用POI读写word docx文件
- 使用POI读写word docx文件
- POI读写Word docx文件
- POI读写Word docx文件
- POI读写Word docx文件
- 使用POI读写word docx文件(笔记记录)
- 使用POI读写word doc文件
- 使用POI读写Word doc文件
- 使用POI读写word doc文件
- 使用POI读写word doc文件
- 使用POI读写word doc文件
- 使用POI读写word doc文件
- 使用POI读写word doc文件
- Java poi 替换 word docx文件
- 使用POI读写Word
- 致身边的亲人朋友,关于 某互助金融平台!!!
- Ehcache缓存详细解读
- (精)JAVA线程池原理以及几种线程池类型介绍
- 硬盘及分区相关常识
- hibernate中oracle库时间格式执行 2015/11/11 15:10:01错误原因
- 使用POI读写word docx文件
- 浅淡C/C++中的typedef和#define
- C#中的Action<T>、Func<T>委托和Lambda表达式
- Qt下如何使用libuv
- 用户体验测试思维
- Maven学习总结(2)——Maven项目构建过程练习
- tableView 分割线顶头
- Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/dsymu
- Android-Interview-Questions