2017-04-07 111 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 **表格中使用合併單元格而不是嵌套表格會是更好的方法。 –

相關問題