2017-04-07 14 views
0

私はセルにテーブルを挿入したい、私はデモを行ったが、失敗、誰かがいくつかのアドバイスを与えることができますか? XWPFDocument.createTableを使用して作成さテーブルにセルを挿入するには?

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

「but but」を意味しますか?すべての例外または何ですか? – Jens

+0

例外はありませんが、tableTwoはtableOneには挿入されていません.tableはtableOneの横に追加されます。 – Sucy

+1

[mcve]を作成してもよろしいですか? – Jens

答えて

2

XWPFTableは常に文書に属します。だから、それは後に文書から取り出されてセルに入れられません。

これを行うにはXWPFTableCell.insertNewTblが必要です。

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(); 

} 
} 

しかし、私は強く代わりにマージされたセルを使用するが、このアプローチを使用しないようお勧めします:

次のコードは、実際のapache poiバージョンを使用して動作します。表のセルに含まれる表は醜いです。これは、HTMLと同様にWordと同様に、テーブルを含むことができる他のすべてのテキストドキュメントフォーマットでも当てはまります。

+0

あなたが正しいです、 'XWPFTableCell.insertNewTable()'は自分のメソッドであり、POIのメソッドではありません。 – jmarkmurphy

+0

私はあなたのコードをテストしました。本当に正常に挿入できます。しかし、私はtableTwoに境界がない理由を知りたいですか? – Sucy

+0

'tableTwo'にはボーダーがありません。' apache poi'は 'insertNewTbl'を使って自動的にそれを設定しないからです。したがって、必要に応じて手動で設定する必要があります。方法を示すためにコードを更新します。しかし、ネストされたテーブルの代わりに** one **テーブル内のマージされたセルを使用する方が良い方法でしょう。 –

関連する問題