使用Apache POI合并Excel中的单元格

时间:2021-05-10 19:10:44

Is there any other way to merge cells in Excel using Apache POI library?

使用Apache POI库合并Excel中的单元格还有其他方法吗?

I was trying using the following, but its not working

我试着用下面的方法,但是没有用

// selecting the region in Worksheet for merging data
CellRangeAddress region = CellRangeAddress.valueOf("A" + rowNo + ":D"
            + rowNo);

// merging the region
sheet1.addMergedRegion(region);

3 个解决方案

#1


92  

You can use sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo);

您可以使用sheet.addMergedRegion(rowFrom、rowTo colFrom,colTo);

example sheet.addMergedRegion(new CellRangeAddress(1,1,1,4)); will merge from B2 to E2. Remember it is zero based indexing (ex. POI version 3.12).

例表。addMergedRegion(新CellRangeAddress(1,1,1,4));将从B2合并到E2。记住它是基于0的索引(例如,POI版本3.12)。

for detail refer BusyDeveloper's Guide

详情请参阅BusyDeveloper指南

#2


3  

The best answer

最好的答案

sheet.addMergedRegion(new CellRangeAddress(start-col,end-col,start-cell,end-cell));

#3


1  

i made a method that merge cells and put border.

我做了一个合并单元格并放置边框的方法。

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

#1


92  

You can use sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo);

您可以使用sheet.addMergedRegion(rowFrom、rowTo colFrom,colTo);

example sheet.addMergedRegion(new CellRangeAddress(1,1,1,4)); will merge from B2 to E2. Remember it is zero based indexing (ex. POI version 3.12).

例表。addMergedRegion(新CellRangeAddress(1,1,1,4));将从B2合并到E2。记住它是基于0的索引(例如,POI版本3.12)。

for detail refer BusyDeveloper's Guide

详情请参阅BusyDeveloper指南

#2


3  

The best answer

最好的答案

sheet.addMergedRegion(new CellRangeAddress(start-col,end-col,start-cell,end-cell));

#3


1  

i made a method that merge cells and put border.

我做了一个合并单元格并放置边框的方法。

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