[英]Apache POI word best way to add text after table
What is the best or short way to add text after table?在表格后添加文本的最佳或简短方法是什么? Not in table but after.
不在桌子上,而是在后面。 The table is in the docx file.
该表位于 docx 文件中。
So, example:所以,例如:
I want to add some text between the Table and textC.我想在 Table 和 textC 之间添加一些文本。 Result:
结果:
I tried following code but it's insert before the table not after.我尝试了以下代码,但它是在表之前而不是之后插入的。
XmlCursor cursor = table.getCTTbl().newCursor();
XWPFParagraph newParagraph = doc.insertNewParagraph(cursor);
XWPFRun run = newParagraph.createRun();
run.setText("inserted new text");
The approach using a XmlCursor is correct.使用XmlCursor的方法是正确的。 Read more about this
XmlCursor
and it's methods in the linked document.在链接的文档中阅读有关此
XmlCursor
及其方法的更多信息。
So we need jumping to the end of the CTTbl
and then finding the next element's start tag.所以我们需要跳到
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();
}
}
this works for me:这对我有用:
tablasolicitantecargo = tablafiladoss.getTable();
org.apache.xmlbeans.XmlCursor cursor = tablasolicitantecargo.getCTTbl().newCursor();
cursor.toEndToken();
while(cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START);
XWPFParagraph newParagraph = requisicionesaprobadas.insertNewParagraph(cursor);
@SuppressWarnings("unused")
XWPFRun run = newParagraph.createRun();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.