package poi;
import java.util.List;
import java.util.Stack;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBookmark;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
*
* Word 文件中标签的封装类,保存了其定义和内部的操作
*
* @author
*
* <p>Modification History:</p>
* <p>Date Author Description</p>
* <p>------------------------------------------------------------------</p>
* <p> </p>
* <p> </p>
*/
public class BookMark {
//以下为定义的常量
/** 替换标签时,设于标签的后面 **/
public static final int INSERT_AFTER = 0;
/** 替换标签时,设于标签的前面 **/
public static final int INSERT_BEFORE = 1;
/** 替换标签时,将内容替换书签 **/
public static final int REPLACE = 2;
/** docx中定义的部分常量引用 **/
public static final String RUN_NODE_NAME = "w:r";
public static final String TEXT_NODE_NAME = "w:t";
public static final String BOOKMARK_START_TAG = "bookmarkStart";
public static final String BOOKMARK_END_TAG = "bookmarkEnd";
public static final String BOOKMARK_ID_ATTR_NAME = "w:id";
public static final String STYLE_NODE_NAME = "w:rPr";
/** 内部的标签定义类 **/
private CTBookmark _ctBookmark = null;
/** 标签所处的段落 **/
private XWPFParagraph _para = null;
/** 标签所在的表cell对象 **/
private XWPFTableCell _tableCell = null;
/** 标签名称 **/
private String _bookmarkName = null;
/** 该标签是否处于表格内 **/
private boolean _isCell = false;
/**
* 构造函数
* @param ctBookmark
* @param para
*/
public BookMark(CTBookmark ctBookmark, XWPFParagraph para) {
this._ctBookmark = ctBookmark;
this._para = para;
this._bookmarkName = ctBookmark.getName();
this._tableCell = null;
this._isCell = false;
}
/**
* 构造函数,用于表格中的标签
* @param ctBookmark
* @param para
* @param tableCell
*/
public BookMark(CTBookmark ctBookmark, XWPFParagraph para, XWPFTableCell tableCell) {
this(ctBookmark, para);
this._tableCell = tableCell;
this._isCell = true;
}
public boolean isInTable() {
return this._isCell;
}
public XWPFTable getContainerTable() {
return this._tableCell.getTableRow().getTable();
}
public XWPFTableRow getContainerTableRow() {
return this._tableCell.getTableRow();
}
public String getBookmarkName() {
return this._bookmarkName;
}
/**
* Insert text into the Word document in the location indicated by this
* bookmark.
*
* @param bookmarkValue An instance of the String class that encapsulates
* the text to insert into the document.
* @param where A primitive int whose value indicates where the text ought
* to be inserted. There are three options controlled by constants; insert
* the text immediately in front of the bookmark (Bookmark.INSERT_BEFORE),
* insert text immediately after the bookmark (Bookmark.INSERT_AFTER) and
* replace any and all text that appears between the bookmark's square
* brackets (Bookmark.REPLACE).
*/
public void insertTextAtBookMark(String bookmarkValue, int where) {
//根据标签的类型,进行不同的操作
if(this._isCell) {
this.handleBookmarkedCells(bookmarkValue, where);
} else {
//普通标签,直接创建一个元素
XWPFRun run = this._para.createRun();
run.setText(bookmarkValue);
switch(where) {
case BookMark.INSERT_AFTER:
this.insertAfterBookmark(run);
break;
case BookMark.INSERT_BEFORE:
this.insertBeforeBookmark(run);
break;
case BookMark.REPLACE:
this.replaceBookmark(run);
break;
}
}
}
/**
* Inserts some text into a Word document in a position that is immediately
* after a named bookmark.
*
* Bookmarks can take two forms, they can either simply mark a location
* within a document or they can do this but contain some text. The
* difference is obvious from looking at some XML markup. The simple
* placeholder bookmark will look like this;
*
* <pre>
*
* <w:bookmarkStart w:name="AllAlone" w:id="0"/><w:bookmarkEnd w:id="0"/>
*
* </pre>
*
* Simply a pair of tags where one tag has the name bookmarkStart, the other
* the name bookmarkEnd and both share matching id attributes. In this case,
* the text will simply be inserted into the document at a point immediately
* after the bookmarkEnd tag. No styling will be applied to the text, it
* will simply inherit the documents defaults.
*
* The more complex case looks like this;
*
* <pre>
*
* <w:bookmarkStart w:name="InStyledText" w:id="3"/>
* <w:r w:rsidRPr="00DA438C">
* <w:rPr>
* <w:rFonts w:hAnsi="Engravers MT" w:ascii="Engravers MT" w:cs="Arimo"/>
* <w:color w:val="FF0000"/>
* </w:rPr>
* <w:t>text</w:t>
* </w:r>
* <w:bookmarkEnd w:id="3"/>
*
* </pre>
*
* Here, the user has selected the word 'text' and chosen to insert a
* bookmark into the document at that point. So, the bookmark tags 'contain'
* a character run that is styled. Inserting any text after this bookmark,
* it is important to ensure that the styling is preserved and copied over
* to the newly inserted text.
*
* The approach taken to dealing with both cases is similar but slightly
* different. In both cases, the code simply steps along the document nodes
* until it finds the bookmarkEnd tag whose ID matches that of the
* bookmarkStart tag. Then, it will look to see if there is one further node
* following the bookmarkEnd tag. If there is, it will insert the text into
* the paragraph immediately in front of this node. If, on the other hand,
* there are no more nodes following the bookmarkEnd tag, then the new run
* will simply be positioned at the end of the paragraph.
*
* Styles are dealt with by 'looking' for a 'w:rPr' element whilst iterating
* through the nodes. If one is found, its details will be captured and
* applied to the run before the run is inserted into the paragraph. If
* there are multiple runs between the bookmarkStart and bookmarkEnd tags
* and these have different styles applied to them, then the style applied
* to the last run before the bookmarkEnd tag - if any - will be cloned and
* applied to the newly inserted text.
*
* @param run An instance of the XWPFRun class that encapsulates the text
* that is to be inserted into the document following the bookmark.
*/
private void insertAfterBookmark(XWPFRun run) {
Node nextNode = null;
Node insertBeforeNode = null;
Node styleNode = null;
int bookmarkStartID = 0;
int bookmarkEndID = -1;
// Capture the id of the bookmarkStart tag. The code will step through
利用poi+word模版书签,向word中插入数据
5星 · 超过95%的资源 需积分: 41 120 浏览量
2013-03-04
15:54:46
上传
评论 14
收藏 21.99MB ZIP 举报
LIU_SHU_DONG
- 粉丝: 1
- 资源: 1
- 1
- 2
- 3
- 4
- 5
- 6
前往页