我正在寻找一种解决方案,无需循环即可将样式应用于一系列单元格。
尝试了在 stackoverflow 上随处可见的其他解决方案,但没有一个有效。 例如,这对我不起作用:
CellRangeAddress region = CellRangeAddress.valueOf("A1:B2");
short borderStyle = CellStyle.BORDER_THIN;
RegionUtil.setBorderBottom(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderTop(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderLeft(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderRight(borderStyle, region, activeSheet, excelWorkbook);
它在选区的外边缘添加边框,而不是在内部的单元格上。 我想为范围内的每个单元格设置边框。如果没有循环,这甚至可能吗?
谢谢
最佳答案
我不认为您可以将样式应用于范围单元格内的所有单元格而不单独将其应用于单个单元格。
尝试遍历每个单元格并应用所有边框。
这是一个可能对您有帮助的示例:
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
for(int i=region.getFirstRow();i<region.getLastRow();i++){
Row row = sheet.getRow(i);
for(int j=region.getFirstColumn();j<region.getLastColumn();j++){
Cell cell = row.getCell(j);
cell.setCellStyle(cellStyle);
}
}
关于java - 将样式应用于单元格范围 - Apache POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37782880/