2017-04-07 118 views
0

我想插入一个表格到一个单元格中,我做了一个演示,但失败了,任何人都可以给一些建议吗?如何将表插入poi单词中的单元格中?

XWPFDocument document = new XWPFDocument(); 
    XWPFTable tableTwo = document.createTable(1,1); 
    XWPFTableRow tableTwoRow1 = tableTwo.getRow(0); 
    tableTwoRow1.getCell(0).setText("aaaaaaaaaa"); 
    XWPFTable tableOne = document.createTable(2,2); 
    XWPFTableRow tableOneRow1 = tableOne.getRow(0); 
    XWPFTableRow tableOneRow2 = tableOne.getRow(1); 
    tableOneRow1.getCell(0).setText("Test"); 
    tableOneRow1.getCell(1).setText("Test"); 
    tableOneRow2.getCell(0).setText("Test"); 
    tableOneRow2.getCell(1).insertTable(0, tableTwo); 
    FileOutputStream fos = new FileOutputStream("D:\\2.docx"); 
    document.write(fos); 
    fos.close(); 
+1

什么意思,但失败?任何异常或什么? – Jens

+0

没有例外,但tableTwo没有插入到tableOne,tableTwo附加在tableOne旁边。 – Sucy

+1

你可以创建一个[mcve]吗? – Jens

回答

2

XWPFTable使用XWPFDocument.createTable永远属于文档创建。所以它不能从文档中取出并放入单元格中。

为此,需要XWPFTableCell.insertNewTbl

下面的代码工作实际使用版本apache poi

import java.io.*; 

import org.apache.poi.xwpf.usermodel.*; 

public class CreateWordTableInTable { 

public static void main(String[] args) throws Exception { 

    XWPFDocument document = new XWPFDocument(); 

    XWPFTable tableOne = document.createTable(2,2); 
    XWPFTableRow tablerow = tableOne.getRow(0); 
    tablerow.getCell(0).setText("Test"); 
    tablerow.getCell(1).setText("Test"); 

    tablerow = tableOne.getRow(1); 
    tablerow.getCell(0).setText("Test"); 

    XWPFParagraph paragraph = tablerow.getCell(1).getParagraphArray(0); 
    XWPFTable tableTwo = tablerow.getCell(1).insertNewTbl(paragraph.getCTP().newCursor()); 

    tableTwo.getCTTbl().addNewTblPr().addNewTblBorders().addNewLeft().setVal(
    org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE); 
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewRight().setVal(
    org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE); 
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewTop().setVal(
    org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE); 
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewBottom().setVal(
    org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE); 
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewInsideH().setVal(
    org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE); 
    tableTwo.getCTTbl().getTblPr().getTblBorders().addNewInsideV().setVal(
    org.openxmlformats.schemas.wordprocessingml.x2006.main.STBorder.SINGLE); 

    tablerow = tableTwo.createRow(); 
    tablerow.createCell().setText("aaaaaaaaaa"); 
    tablerow.createCell().setText("jjjjjjjj"); 
    tablerow = tableTwo.createRow(); 
    tablerow.getCell(0).setText("bbbbbbbbbb"); 
    tablerow.getCell(1).setText("gggggggggg"); 

    document.write(new FileOutputStream("CreateWordTableInTable.docx")); 
    document.close(); 

} 
} 

但我会强烈建议不要使用这种方法,但使用合并单元格来代替。表格单元格中包含的表格很难看。对于HTML以及Word以及所有其他可以包含表格的文本文档格式都是如此。

+0

你说得对,'XWPFTableCell.insertNewTable()'是我自己的方法,而不是POI中的一个。 – jmarkmurphy

+0

我测试了你的代码,tableTwo真的可以插入成功。但我想知道为什么tableTwo没有边界? – Sucy

+0

'tableTwo'没有边框,因为'apache poi'不会使用'insertNewTbl'自动设置它。所以你必须手动设置它,如果需要的话。我将更新我的代码以显示如何。但如前所述,在** one **表格中使用合并单元格而不是嵌套表格会是更好的方法。 –

相关问题