慕運維8079593
2019-01-19 01:15:03
poi 合并word中的表格的單元格時,跨行可以正確合并,跨列合并則失敗, 以下是合并列的代碼
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);
}
}
}
有知道為什么的嗎?
3 回答

楊魅力
TA貢獻1811條經(jīng)驗 獲得超6個贊
跨列用到的是addNewHMerge(),跨行用到的是addNewVMerge()。我最近也在用poi導出word,仔細觀察發(fā)現(xiàn)其實是兩個不同的方法,要仔細啊兄dei

慕少森
TA貢獻2019條經(jīng)驗 獲得超9個贊
合并后的列需要設置相應的寬度。
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
后面加上
//cellCount為表格總列數(shù)
Integer width = (toCell-fromCell+1)/cellCount*table.getCTTbl().getTblPr().getTblW().getW().intValue();
cell.getCTTc().getTcPr().addNewTcW().setW(BigInteger.valueOf(width));
添加回答
舉報
0/150
提交
取消