Is there a way to add borders to a cellrange using Java with Apache POI?
是否有一种方法可以使用Java和Apache POI向cellrange添加边框?
Like A1:B2 should get a top-bottom-left-right thick border - style?
像A1:B2应该有上下左右的厚边框样式?
I know how to create & apply styles to single cells and I might iterate trough the cells and apply the appropriate styles but I'm sure there's an easier way.
我知道如何在单个单元格中创建和应用样式,我可能会遍历这些单元格并应用适当的样式,但我确信有一种更简单的方法。
3 个解决方案
#1
11
I've been able to figure it out. There is actually a sample on the apache poi page I just didn't find with the keywords I've been searching with.
我已经弄明白了。实际上,在apache poi页面上有一个示例,我只是在搜索关键字时没有找到它。
CellRangeAddress region = CellRangeAddress.valueOf(A1:B2);
short borderStyle = CellStyle.BORDER_MEDIUM;
RegionUtil.setBorderBottom(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderTop(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderLeft(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderRight(borderStyle, region, activeSheet, excelWorkbook);
#2
3
Things have changed in 3.16
3.16的情况发生了变化。
CellRangeAddress region = new CellRangeAddress(6, 8, 1, 10);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
#3
0
RegionUtil turned all background colors other than white to black in my case. This is my workaround:
在我的案例中,区域化把所有背景色都变成了黑色。这是我的解决方案:
public final class BorderUtils {
public static void setBorder(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
setBorderTop(sheet, borderStyle, region);
setBorderBottom(sheet, borderStyle, region);
setBorderLeft(sheet, borderStyle, region);
setBorderRight(sheet, borderStyle, region);
}
public static void setBorderTop(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
Row row = sheet.getRow(region.getFirstRow());
for (int i=region.getFirstColumn() ; i<=region.getLastColumn() ; i++) {
Cell cell = row.getCell(i);
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderTop(borderStyle);
cell.setCellStyle(cellStyle);
}
}
public static void setBorderBottom(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
Row row = sheet.getRow(region.getLastRow());
for (int i=region.getFirstColumn() ; i<=region.getLastColumn() ; i++) {
Cell cell = row.getCell(i);
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderBottom(borderStyle);
cell.setCellStyle(cellStyle);
}
}
public static void setBorderLeft(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
for (int i=region.getFirstRow() ; i<=region.getLastRow() ; i++) {
Cell cell = sheet.getRow(i).getCell(region.getFirstColumn());
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderLeft(borderStyle);
cell.setCellStyle(cellStyle);
}
}
public static void setBorderRight(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
for (int i=region.getFirstRow() ; i<=region.getLastRow() ; i++) {
Cell cell = sheet.getRow(i).getCell(region.getLastColumn());
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderRight(borderStyle);
cell.setCellStyle(cellStyle);
}
}
}
#1
11
I've been able to figure it out. There is actually a sample on the apache poi page I just didn't find with the keywords I've been searching with.
我已经弄明白了。实际上,在apache poi页面上有一个示例,我只是在搜索关键字时没有找到它。
CellRangeAddress region = CellRangeAddress.valueOf(A1:B2);
short borderStyle = CellStyle.BORDER_MEDIUM;
RegionUtil.setBorderBottom(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderTop(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderLeft(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderRight(borderStyle, region, activeSheet, excelWorkbook);
#2
3
Things have changed in 3.16
3.16的情况发生了变化。
CellRangeAddress region = new CellRangeAddress(6, 8, 1, 10);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);
#3
0
RegionUtil turned all background colors other than white to black in my case. This is my workaround:
在我的案例中,区域化把所有背景色都变成了黑色。这是我的解决方案:
public final class BorderUtils {
public static void setBorder(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
setBorderTop(sheet, borderStyle, region);
setBorderBottom(sheet, borderStyle, region);
setBorderLeft(sheet, borderStyle, region);
setBorderRight(sheet, borderStyle, region);
}
public static void setBorderTop(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
Row row = sheet.getRow(region.getFirstRow());
for (int i=region.getFirstColumn() ; i<=region.getLastColumn() ; i++) {
Cell cell = row.getCell(i);
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderTop(borderStyle);
cell.setCellStyle(cellStyle);
}
}
public static void setBorderBottom(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
Row row = sheet.getRow(region.getLastRow());
for (int i=region.getFirstColumn() ; i<=region.getLastColumn() ; i++) {
Cell cell = row.getCell(i);
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderBottom(borderStyle);
cell.setCellStyle(cellStyle);
}
}
public static void setBorderLeft(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
for (int i=region.getFirstRow() ; i<=region.getLastRow() ; i++) {
Cell cell = sheet.getRow(i).getCell(region.getFirstColumn());
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderLeft(borderStyle);
cell.setCellStyle(cellStyle);
}
}
public static void setBorderRight(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
for (int i=region.getFirstRow() ; i<=region.getLastRow() ; i++) {
Cell cell = sheet.getRow(i).getCell(region.getLastColumn());
CellStyle cellStyle = cloneCellStyle(sheet, cell);
cellStyle.setBorderRight(borderStyle);
cell.setCellStyle(cellStyle);
}
}
}