有没有其他方法可以使用Apache POI库在Excel中合并单元格?
我正在尝试使用以下内容,但它无效
// selecting the region in Worksheet for merging data
CellRangeAddress region = CellRangeAddress.valueOf("A" + rowNo + ":D"
+ rowNo);
// merging the region
sheet1.addMergedRegion(region);
答案 0 :(得分:123)
您可以使用sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo);
示例sheet.addMergedRegion(new CellRangeAddress(1,1,1,4));
将从B2合并到E2。请记住,它是基于零的索引(例如POI版本3.12)。
答案 1 :(得分:6)
最佳答案
sheet.addMergedRegion(new CellRangeAddress(start-col,end-col,start-cell,end-cell));
答案 2 :(得分:4)
您可以使用:
sheet.addMergedRegion(new CellRangeAddress(startRowIndx, endRowIndx, startColIndx,endColIndx));
确保CellRangeAddress与其他合并的区域不一致,因为这将引发异常。
对于您尝试执行的操作应该可以:
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 3));
答案 3 :(得分:2)
我做了一个合并单元格并放置边框的方法。
protected void setMerge(Sheet sheet, int numRow, int untilRow, int numCol, int untilCol, boolean border) {
CellRangeAddress cellMerge = new CellRangeAddress(numRow, untilRow, numCol, untilCol);
sheet.addMergedRegion(cellMerge);
if (border) {
setBordersToMergedCells(sheet, cellMerge);
}
}
protected void setBordersToMergedCells(Sheet sheet, CellRangeAddress rangeAddress) {
RegionUtil.setBorderTop(BorderStyle.MEDIUM, rangeAddress, sheet);
RegionUtil.setBorderLeft(BorderStyle.MEDIUM, rangeAddress, sheet);
RegionUtil.setBorderRight(BorderStyle.MEDIUM, rangeAddress, sheet);
RegionUtil.setBorderBottom(BorderStyle.MEDIUM, rangeAddress, sheet);
}
答案 4 :(得分:0)
语法是:
sheet.addMergedRegion(new CellRangeAddress(start-col,end-col,start-cell,end-cell));
示例:
sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 5));
这里单元格0到单元格5将合并到第四行。