java 使用apache poi将表格插入到特定位置的word文档中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31835579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert table into a word document at a specific position using apache poi
提问by dharlequin
I am working on a project where I am trying to create an automated report generator. I need to pinpoint a couple of specific paragraphs, eliminate the tables that are already there and insert a new table.
我正在一个项目上工作,我试图在其中创建一个自动报告生成器。我需要找出几个特定的段落,删除已经存在的表格并插入一个新表格。
Everything up to this point works perfect. I even manage to insert a sample text in the place that I want, but... all the tables are placed at the end of the document, despite what I do.
到目前为止,一切都很完美。我什至设法在我想要的地方插入了一个示例文本,但是......所有的表格都放在文档的末尾,尽管我做了什么。
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);
}
}
}
回答by Clebarin Miforn
//first row
XWPFTableRow rowOfOriginalTable = theOriginalTable.getRow(0);
//second cell of the first row
XWPFTableCell cellOfOriginalTable = rowOfOriginalTable.getCell(1);
//new paragraph in that cell
XWPFParagraph p = cellOfOriginalTable.addParagraph();
//get the cursor of the new paragraph
XmlCursor cursor = p.getCTP().newCursor();
//add the nested Table
XWPFTable nestedTable = p.getBody().insertNewTbl(cursor);
//add the first row to the nested table
XWPFTableRow rowOfNestedTable = nestedTable.createRow();
//add a cell to the first row
XWPFTableCell cellOfNestedTable = rowOfNestedTable.createCell();
//add a value
cellOfNestedTable.setText("Cell 0,0");
//add another cell
cellOfNestedTable = rowOfNestedTable.createCell();
cellOfNestedTable.setText("Cell 0,1");
//add another cell and rows
rowOfNestedTable = nestedTable.createRow();
cellOfNestedTable = rowOfNestedTable.getCell(0);
cellOfNestedTable.setText("Cell 1,0");
cellOfNestedTable = rowOfNestedTable.getCell(1);
cellOfNestedTable.setText("Cell 1,1");
cellOfOriginalTable.addParagraph();
回答by dharlequin
As it turns out, you can't use one cursor for multiple purposes, so all I needed to do was to create a fresh cursor for my future table.
事实证明,您不能将一个游标用于多种用途,所以我需要做的就是为我未来的表创建一个新的游标。
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!!!");
回答by Baked Inhalf
This will insert a table at a given position:
这将在给定位置插入一个表格:
CTTbl inserted = doc.getDocument().getBody().insertNewTbl(position);
XWPFTable newTable = new XWPFTable(inserted, doc);
Where doc
is an XWPFDocument object and position
is your position among other tables.
doc
XWPFDocument 对象在哪里以及position
您在其他表格中的位置。