使用XmlCursor的方法是正確的。閱讀更多關於這個XmlCursor
和鏈接文檔中的方法。
所以我們需要跳到CTTbl
的末尾,然後找到下一個元素的開始標籤。
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;
public class WordTextAfterTable {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream("WordTextAfterTable.docx"));
XWPFTable table = document.getTableArray(0);
org.apache.xmlbeans.XmlCursor cursor = table.getCTTbl().newCursor();
cursor.toEndToken(); //now we are at end of the CTTbl
//there always must be a next start token. Either a p or at least sectPr.
while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START);
XWPFParagraph newParagraph = document.insertNewParagraph(cursor);
XWPFRun run = newParagraph.createRun();
run.setText("inserted new text");
document.write(new FileOutputStream("WordTextAfterTableNew.docx"));
document.close();
}
}
在表格後面創建'XWPFParagraph',然後'XWPFRun'包含本段中的文本。 –
好的,但我怎樣才能設置XWPFParagraph的位置?我試過這個:XmlCursor cursor = table.getCTTbl()。newCursor()但是表格的前面位置。 – Zaosz
請編輯您的問題並顯示您正在使用的代碼。還要詳細解釋你在做什麼。桌子從哪裏來?你怎麼弄桌子? –