【问题标题】:Insert a table inside a bookmark using Apache POI使用 Apache POI 在书签内插入表格
【发布时间】:2016-05-02 14:56:55
【问题描述】:

我将 Apache POI 与 Jython 一起使用来创建表格并将它们放置在我有书签的 docx 的某些位置。我可以通过它们的名称找到书签(CTBookmark 对象),在放置它们的段落的开头创建一个光标,然后在那里创建一个新表:

cursor = para.getCTP().newCursor() #para is the paragraph where the bookmark is placed
table = document.insertNewTbl(cursor) #cursor is an XMLCursor

如果我只想插入我可以使用的文本:

nextNode = bookmark.getDomNode() #considering it is the node named 'bookmarkEnd'
run = para.createRun()
run.setText('foo')
para.getCTP().getDomNode().insertBefore(run.getCTR().getDomNode(),nextNode)

但是要插入另一个元素,比如表格,我找不到解决方案。如果表格放在书签内会更好,但如果将它放在它之前,而不是在段落的开头,那也很棒。

感谢任何帮助或替代想法。谢谢。

【问题讨论】:

    标签: apache-poi jython docx bookmarks xwpf


    【解决方案1】:

    差不多了,您需要创建行和单元格。

    注册示例。

    希望能帮到你。

    XWPFTable table = doc.insertNewTbl(cursor);
    for(int rowIndex=0; rowIndex < 3; rowIndex++){
        String line = "LineContent "+rowIndex;
        XWPFTableRow row = table.getRow(rowIndex);
    
        if(row==null){
            row = table.createRow();       
        }
    
        for(int colIndex=0; colIndex < 2; colIndex++){
            XWPFTableCell  cell = row.getCell(colIndex);
            if(cell == null){
              cell = row.createCell();
            }
    
            cell.setText(line+" Col "+colIndex);
         }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-02
      • 2012-09-27
      相关资源
      最近更新 更多