Java 使用ApachePOI将表插入word文档中的特定位置
我正在从事一个项目,我正在尝试创建一个自动报告生成器。我需要指出几个具体的段落,删除已经存在的表并插入一个新表 到目前为止,一切都很完美。我甚至在我想要的地方插入了一个示例文本,但是。。。不管我怎么做,所有的表格都放在文档的末尾Java 使用ApachePOI将表插入word文档中的特定位置,java,ms-word,apache-poi,xwpf,Java,Ms Word,Apache Poi,Xwpf,我正在从事一个项目,我正在尝试创建一个自动报告生成器。我需要指出几个具体的段落,删除已经存在的表并插入一个新表 到目前为止,一切都很完美。我甚至在我想要的地方插入了一个示例文本,但是。。。不管我怎么做,所有的表格都放在文档的末尾 public class InsertText { public static void main(String[] args) throws FileNotFoundException, IOException, InvalidForm
public class InsertText {
public static void main(String[] args) throws FileNotFoundException, IOException,
InvalidFormatException {
try {
FileInputStream fis = new FileInputStream("c:\\Work\\current\\***.docx");
XWPFDocument document = new XWPFDocument(OPCPackage.open(fis));
fis.close();
System.out.println(document.getDocument().getBody().getPArray().length);
List<IBodyElement> elements = document.getBodyElements();
for (int n = 0; n < elements.size(); n++) {
IBodyElement element = elements.get(n);
if (element instanceof XWPFParagraph) {
XWPFParagraph p1 = (XWPFParagraph) element;
List<XWPFRun> runList = p1.getRuns();
StringBuilder sb = new StringBuilder();
for (XWPFRun run : runList)
sb.append(run.getText(0));
if (sb.toString().contains("????")) {
n++;
element = elements.get(n);
if (element instanceof XWPFTable) {
XWPFTable t = (XWPFTable) element;
XmlCursor cursor = t.getCTTbl().newCursor();
document.removeBodyElement(n);
XWPFParagraph p = document.insertNewParagraph(cursor);
XWPFRun run = p.createRun();
run.setText("GOAL!!!");
XWPFTable t2 = document.createTable(3,4);
XWPFTableCell cell = t2.getRow(0).getCell(0);
document.insertTable(n, t2);
cell.setText("GOAL!!!");
t2 = p.getBody().insertNewTbl(cursor);
}
}
}
}
FileOutputStream outStream = new FileOutputStream("C:/Work/Current/**.docx");
document.write(outStream);
outStream.close();
} catch (Exception e) {
e.printStackTrace(System.out);
}
}
}
公共类插入文本{
公共静态void main(字符串[]args)抛出FileNotFoundException、IOException、,
无效格式异常{
试一试{
FileInputStream fis=新的FileInputStream(“c:\\Work\\current\\\***.docx”);
XWPFDocument document=新的XWPFDocument(OPCPackage.open(fis));
fis.close();
System.out.println(document.getDocument().getBody().getPArray().length);
列表元素=document.getBodyElements();
对于(int n=0;n
事实证明,不能将一个游标用于多个目的,所以我需要做的就是为我未来的表创建一个新的游标
run.setText("GOAL!!!");
cursor = p.getCTP().newCursor();//this is the key!
XWPFTable t2 = document.insertNewTbl(cursor);
XWPFTableCell cell = t2.getRow(0).getCell(0);
cell.setText("GOAL!!!");
事实证明,不能将一个游标用于多个目的,所以我所需要做的就是为我未来的表创建一个新的游标
run.setText("GOAL!!!");
cursor = p.getCTP().newCursor();//this is the key!
XWPFTable t2 = document.insertNewTbl(cursor);
XWPFTableCell cell = t2.getRow(0).getCell(0);
cell.setText("GOAL!!!");
这将在给定位置插入表格:
CTTbl inserted = doc.getDocument().getBody().insertNewTbl(position);
XWPFTable newTable = new XWPFTable(inserted, doc);
其中,
doc
是XWPFDocument对象,position
是您在其他表中的位置 这将在给定位置插入表格:
CTTbl inserted = doc.getDocument().getBody().insertNewTbl(position);
XWPFTable newTable = new XWPFTable(inserted, doc);
其中,doc
是XWPFDocument对象,position
是您在其他表中的位置