1.POI引用:
org.apache.poi
poi-ooxml
3.15
org.apache.poi
ooxml-schemas
1.3
org.apache.poi
poi-ooxml-schemas
3.15
org.apache.poi
poi-scratchpad
3.15
2.替换段落标签
功能代码如下:
File fileTem= new File(officeWordPath() + File.separator +tMobileWordSign.getWordPath());
InputStream iss= newFileInputStream(fileTem);
XWPFDocument doc= newXWPFDocument(iss);
DateFormat dateFormat= new SimpleDateFormat("yyyy 年 MM 月 dd 日");
List xwpfParagraphList =doc.getParagraphs();//基本内容替换
for(XWPFParagraph xwpfParagraph : xwpfParagraphList) {
CTP ctp=xwpfParagraph.getCTP();for (int dwI = 0; dwI < ctp.sizeOfBookmarkStartArray(); dwI++) {
CTBookmark bookmark=ctp.getBookmarkStartArray(dwI);if (bookmark.getName().equals("year")) {
XWPFRun run=xwpfParagraph.createRun();
run.setText(dateFormat.format(tMobileWordSign.getSignDate()));
Node firstNode=bookmark.getDomNode();
Node nextNode=firstNode.getNextSibling();while (nextNode != null) {//循环查找结束符
String nodeName =nextNode.getNodeName();if(nodeName.equals(BOOKMARK_END_TAG)) {break;
}//删除中间的非结束节点,即删除原书签内容