java 你能在table里做一个table吗?

bis0qfac  于 2022-12-25  发布在  Java
关注(0)|答案(1)|浏览(198)

你能像案例2一样在table里面的隔间里做一张table吗?你能像案例2一样在table里面的隔间里做一张table吗?

enter image description here

public class PoiTest3 {

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

        try (XWPFDocument doc = new XWPFDocument()) {

            XWPFTable table = doc.createTable();

            //Creating first Row
            XWPFTableRow row1 = table.getRow(0);
            row1.getCell(0).setText("First Row, First Column");
            row1.addNewTableCell().setText("First Row, Second Column");
            row1.addNewTableCell().setText("First Row, Third Column");

            //Creating second Row
            XWPFTableRow row2 = table.createRow();
            row2.getCell(0).setText("Second Row, First Column");
            row2.getCell(1).setText("Second Row, Second Column");
            row2.getCell(2).setText("Second Row, Third Column");

            //create third row
            XWPFTableRow row3 = table.createRow();
            row3.getCell(0).setText("Third Row, First Column");
            row3.getCell(1).setText("Third Row, Second Column");
            row3.getCell(2).setText("Third Row, Third Column");

            // save to .docx file
            try (FileOutputStream out = new FileOutputStream("c:\\excel\\table.docx")) {
                doc.write(out);
            }

        }

    }

}
11dmarpk

11dmarpk1#

XWPFTableCell是一个IBody,所以它提供了XWPFTable insertNewTbl(org.apache.xmlbeans.XmlCursor游标),所以是的,可以在表格单元格中添加表格。
但是org.apache.xmlbeans.XmlCursor的用法没有很好的文档记录。
要创建该光标,我们需要表单元格,光标应位于其中,并且在该单元格中有一个新的空段落。之所以需要空段落,是因为我们使用该光标插入的所有内容都将位于光标所指向的元素之前。因此,该元素应为空段落,以避免将内容插入现有元素中,如具有内容的段落或包含元素的其他内容。
下面显示了一个最简单的完整示例。

import java.io.FileOutputStream;

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

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

  try (XWPFDocument doc = new XWPFDocument()) {

   //create main table
   XWPFTable table = doc.createTable();

   //create rows and cells
   XWPFTableRow row = table.getRow(0);
   row.getCell(0).setText("Main table A1");
   row.addNewTableCell().setText("Main table B1");
   row.addNewTableCell().setText("Main table C1");
   row = table.createRow();
   row.getCell(0).setText("Main table A2");
   row.getCell(1).setText("Main table B2");
   row.getCell(2).setText("Main table C2");
   
   
   //create inner table
   //we need the first table cell and an new empty paragrsph in that first cell
   row = table.getRow(0);
   XWPFTableCell cell = row.getTableCells().get(0);
   XWPFParagraph paragraph = cell.addParagraph();
   //now we can insert a table there
   org.apache.xmlbeans.XmlCursor cursor = paragraph.getCTP().newCursor();
   XWPFTable innerTable = cell.insertNewTbl(cursor);
   //set table borders
   innerTable.setTopBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");
   innerTable.setRightBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");
   innerTable.setBottomBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");
   innerTable.setLeftBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");
   innerTable.setInsideHBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");
   innerTable.setInsideVBorder(XWPFTable.XWPFBorderType.SINGLE, 4, 0, "000000");
   //create rows and cells
   XWPFTableRow rowInInnerTable = innerTable.createRow();
   XWPFTableCell cellInInnerTable = rowInInnerTable.createCell();
   cellInInnerTable.setText("Inner table A1");
   cellInInnerTable = rowInInnerTable.createCell();
   cellInInnerTable.setText("Inner table B1");
   cellInInnerTable = rowInInnerTable.createCell();
   cellInInnerTable.setText("Inner table C1");
   rowInInnerTable = innerTable.createRow();
   cellInInnerTable = rowInInnerTable.getCell(0);
   cellInInnerTable.setText("Inner table A2");
   cellInInnerTable = rowInInnerTable.getCell(1);
   cellInInnerTable.setText("Inner table B2");
   cellInInnerTable = rowInInnerTable.getCell(2);
   cellInInnerTable.setText("Inner table C2");
    
   //save to .docx file
   try (FileOutputStream out = new FileOutputStream("./CreateWordTableInTable.docx")) {
    doc.write(out);
   }
  }
 }
}

它产生:

此代码使用当前Apache POI版本5.2.3进行了测试和工作。下载:https://poi.apache.org/download.html#POI-5.2.3。所需组件请参见https://poi.apache.org/components/index.html#components。

相关问题