[简单]poi word2007表格单元格合并

        代码参考了http://stackoverflow.com/questions/24907541/row-span-with-xwpftable,代码如下

       

import java.io.FileOutputStream;
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.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STJc;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalJc;

public class POI_表格合并_S3_Test {
	public static void main(String[] args) throws Exception {
		POI_表格合并_S3_Test t=new POI_表格合并_S3_Test();
		XWPFDocument document = new XWPFDocument();
		t.megerTableCell(document);
		t.saveDocument(document, "f:/saveFile/temp/sys_"+ System.currentTimeMillis() + ".docx");
	}

	public void megerTableCell(XWPFDocument document) {
		XWPFTable table1 = document.createTable(6, 8); 
		setTableWidth(table1, "8000");
        fillTable(table1);
        mergeCellsVertically(table1, 1, 1,4);
        mergeCellsVertically(table1, 4, 2, 4);
        mergeCellsHorizontal(table1,0,3,5);
        mergeCellsHorizontal(table1,2,2,3);
        mergeCellsHorizontal(table1,2,6,7);
	}

	public  void fillTable(XWPFTable table) {
        for (int rowIndex = 0; rowIndex < table.getNumberOfRows(); rowIndex++) {
            XWPFTableRow row = table.getRow(rowIndex);
            row.setHeight(380);
            for (int colIndex = 0; colIndex < row.getTableCells().size(); colIndex++) {
                XWPFTableCell cell = row.getCell(colIndex);
                if(rowIndex%2==0){
                	 setCellText(cell, " cell " + rowIndex + colIndex + " ", "D4DBED", 1000);
                }else{
                	 setCellText(cell, " cell " + rowIndex + colIndex + " ", "AEDE72", 1000);
                }
            }
        }
    }
	
	public  void setCellText(XWPFTableCell cell,String text, String bgcolor, int width) {
		CTTc cttc = cell.getCTTc();
		CTTcPr cellPr = cttc.addNewTcPr();
		cellPr.addNewTcW().setW(BigInteger.valueOf(width));
		//cell.setColor(bgcolor);
		CTTcPr ctPr = cttc.addNewTcPr();
		CTShd ctshd = ctPr.addNewShd();
		ctshd.setFill(bgcolor);
		ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
		cttc.getPList().get(0).addNewPPr().addNewJc().setVal(STJc.CENTER);
		cell.setText(text);
	}
	

	/**
	 * @Description: 跨列合并
	 */
	public  void mergeCellsHorizontal(XWPFTable table, int row, int fromCell, int toCell) {
        for (int cellIndex = fromCell; cellIndex <= toCell; cellIndex++) {
            XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
            if ( cellIndex == fromCell ) {
                // 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);
            }
        }
    }
	
	/**
	 * @Description: 跨行合并
	 * @see http://stackoverflow.com/questions/24907541/row-span-with-xwpftable
	 */
    public  void mergeCellsVertically(XWPFTable table, int col, int fromRow, int toRow) {
        for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
            XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
            if ( rowIndex == fromRow ) {
                // 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);
            }
        }
    }
    
	public void setTableWidth(XWPFTable table,String width){
		CTTbl ttbl = table.getCTTbl();
		CTTblPr tblPr = ttbl.getTblPr() == null ? ttbl.addNewTblPr() : ttbl.getTblPr();
		CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
		CTJc cTJc=tblPr.addNewJc();
		cTJc.setVal(STJc.Enum.forString("center"));
		tblWidth.setW(new BigInteger(width));
		tblWidth.setType(STTblWidth.DXA);
	}
	
	public void saveDocument(XWPFDocument document, String savePath)
			throws Exception {
		FileOutputStream fos = new FileOutputStream(savePath);
		document.write(fos);
		fos.close();
	}
}

   结果如下

  

 

   全文完

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要获取Word Doc中表格合并单元,可以使用Apache POI库中的XWPFTable类和XWPFTableCell类。 以下是一个示例代码,可以通过它来获取表格中的合并单元: ```java import org.apache.poi.xwpf.usermodel.*; import java.util.List; public class WordTableMergeCellsExample { public static void main(String[] args) throws Exception { // Load the document XWPFDocument doc = new XWPFDocument(new FileInputStream("example.docx")); // Get the first table in the document XWPFTable table = doc.getTables().get(0); // Iterate through the rows in the table for (XWPFTableRow row : table.getRows()) { // Iterate through the cells in the row for (XWPFTableCell cell : row.getTableCells()) { // Get the list of merged cells List<XWPFTableCell> mergedCells = table.getTableCellsBeingMerged(cell); // If the current cell is merged with other cells if (mergedCells.size() > 1) { System.out.println("Cell at row " + row.getRowNum() + ", column " + cell.getColumnIndex() + " is merged with:"); // Print the merged cells for (XWPFTableCell mergedCell : mergedCells) { System.out.println(" Row " + table.getRow(mergedCell).getRowNum() + ", column " + mergedCell.getColumnIndex()); } } } } // Close the document doc.close(); } } ``` 该代码将遍历表格中的所有单元,并检查每个单元是否与其他单元合并。如果单元合并,则该代码将打印出该单元合并单元的位置信息。 请注意,该代码假定文档中至少有一个表格。如果您的文档中没有表格,或者您想获取其他表格中的合并单元,请相应地修改代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值