POI Word單元格合併

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

}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章