POI Word单元格合并_小百菜的博客-CSDN博客

1、pom.xml

<dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi-ooxml</artifactId>    <version>4.1.2</version></dependency><dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi-ooxml-schemas</artifactId>    <version>4.1.2</version></dependency><dependency>    <groupId>org.apache.poi</groupId>    <artifactId>poi</artifactId>    <version>4.1.2</version></dependency><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-collections4</artifactId>    <version>4.4</version></dependency><dependency>    <groupId>commons-codec</groupId>    <artifactId>commons-codec</artifactId>    <version>1.13</version></dependency><dependency>    <groupId>org.apache.commons</groupId>    <artifactId>commons-compress</artifactId>    <version>1.19</version></dependency><dependency>    <groupId>org.apache.xmlbeans</groupId>    <artifactId>xmlbeans</artifactId>    <version>3.1.0</version></dependency>

2、单元格合并

package test; import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.math.BigInteger; import org.apache.poi.xwpf.usermodel.XWPFDocument;import org.apache.poi.xwpf.usermodel.XWPFTable;import org.apache.poi.xwpf.usermodel.XWPFTableCell;import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge; public class Test {     public static void main(String[] args) {        XWPFDocument document = new XWPFDocument();        FileOutputStream fos = null;        try {            fos = new FileOutputStream(new File("I:\\test.docx"));            // 创建一个11行11列的表格            XWPFTable table = document.createTable(11, 11);            // 表格宽度            CTTblWidth width = table.getCTTbl().addNewTblPr().addNewTblW();            width.setW(BigInteger.valueOf(2000));             //列合并参数:表格,行,开始列,结束列            mergeCellsHorizontal(table, 0, 1, 2);            //列合并参数:表格,行,开始列,结束列            mergeCellsHorizontal(table, 0, 4, 5);            //列合并参数:表格,行,开始列,结束列            mergeCellsHorizontal(table, 0, 7, 8);             //行合并参数:表格,列,开始行,结束行            mergeCellsVertically(table, 0, 1, 10);             document.write(fos);            System.out.println("successully");        } catch (Exception e) {            e.printStackTrace();        } finally {            if (fos != null) {                try {                    fos.close();                    document.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }     /**     * word单元格列合并     * @param table 表格     * @param row 合并列所在行     * @param startCell 开始列     * @param endCell 结束列     * @date 2020年4月8日 下午4:43:54     */    public static void mergeCellsHorizontal(XWPFTable table, int row, int startCell, int endCell) {        for (int i = startCell; i <= endCell; i++) {            XWPFTableCell cell = table.getRow(row).getCell(i);            if (i == startCell) {                // The first merged cell is set with RESTART merge value                  cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);            } else {                // Cells which join (merge) the first one, are set with CONTINUE                  cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);            }        }    }     /**     * word单元格行合并     * @param table 表格     * @param col 合并行所在列     * @param fromRow 开始行     * @param toRow 结束行     * @date 2020年4月8日 下午4:46:18     */    public static void mergeCellsVertically(XWPFTable table, int col, int startRow, int endRow) {        for (int i = startRow; i <= endRow; i++) {            XWPFTableCell cell = table.getRow(i).getCell(col);            if (i == startRow) {                // The first merged cell is set with RESTART merge value                  cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);            } else {                // Cells which join (merge) the first one, are set with CONTINUE                  cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);            }        }    } }

原网址: 访问
创建于: 2023-04-04 16:28:40
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论