首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用apache poi在word文档中特定位置插入表格

使用apache poi在word文档中特定位置插入表格
EN

Stack Overflow用户
提问于 2015-08-05 22:36:13
回答 3查看 13.2K关注 0票数 1

我正在从事一个项目,其中我试图创建一个自动报告生成器。我需要找出几个特定的段落,删除已经存在的表,然后插入一个新的表。

到目前为止,一切都运行得很完美。我甚至设法在我想要的地方插入了一个示例文本,但是...不管我怎么做,所有的表格都放在文档的末尾。

代码语言:javascript
运行
复制
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);
        }
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-08-06 15:25:45

事实证明,您不能将一个游标用于多个目的,所以我所需要做的就是为我将来的表创建一个新的游标。

代码语言:javascript
运行
复制
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!!!");
票数 1
EN

Stack Overflow用户

发布于 2015-11-23 19:23:55

代码语言:javascript
运行
复制
//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();
票数 6
EN

Stack Overflow用户

发布于 2018-01-05 19:23:24

这将在给定位置插入一个表:

代码语言:javascript
运行
复制
CTTbl inserted = doc.getDocument().getBody().insertNewTbl(position);    
XWPFTable newTable = new XWPFTable(inserted, doc);

其中doc是XWPFDocument对象,position是您在其他表中的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31835579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档