我目前正在使用xlsx
将合并的区域添加到我的Apache POI
文件中。我正在使用RegionUtil
为跨越多个列和行的合并单元格创建边框。
有关选择正确的边框样式的部分工作正常,但颜色本身为黑色(而不是来自输入awt.Color
的预期灰色)。 API仅提及int
颜色,我错误地认为它们表示索引:https://poi.apache.org/apidocs/dev/org/apache/poi/ss/util/RegionUtil.html
如何提供正确的int
值(基于输入awt.Color
),以便边框颜色与普通单元格样式中的边框颜色相同?
设置合并边框的代码
private void setBordersToMergedCells(XSSFSheet in_sheet, java.awt.Color in_bordercolor) {
int numMerged = in_sheet.getNumMergedRegions();
for (int i = 0; i < numMerged; i++) {
//this works
CellRangeAddress mergedRegions = in_sheet.getMergedRegion(i);
RegionUtil.setBorderLeft(BorderStyle.THIN, mergedRegions, in_sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, mergedRegions, in_sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, mergedRegions, in_sheet);
RegionUtil.setBorderBottom(BorderStyle.THIN, mergedRegions, in_sheet);
//this does not work
RegionUtil.setLeftBorderColor(new XSSFColor(in_bordercolor).getIndex(), mergedRegions, in_sheet);
RegionUtil.setRightBorderColor(new XSSFColor(in_bordercolor).getIndex(), mergedRegions, in_sheet);
RegionUtil.setTopBorderColor(new XSSFColor(in_bordercolor).getIndex(), mergedRegions, in_sheet);
RegionUtil.setBottomBorderColor(new XSSFColor(in_bordercolor).getIndex(), mergedRegions, in_sheet);
}
为了进行比较,这是我如何使用(给定的)awt颜色(完美工作)设置单元格样式的方法:
protected XSSFCellStyle createBorderedStyleXLSX(Workbook in_wb, java.awt.Color in_bordercolor) {
XSSFCellStyle style = (XSSFCellStyle) in_wb.createCellStyle();
style.setBorderTop(BorderStyle.THIN);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
style.setBorderColor(XSSFCellBorder.BorderSide.TOP, new XSSFColor(in_bordercolor));
style.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, new XSSFColor(in_bordercolor));
style.setBorderColor(XSSFCellBorder.BorderSide.LEFT, new XSSFColor(in_bordercolor));
style.setBorderColor(XSSFCellBorder.BorderSide.RIGHT, new XSSFColor(in_bordercolor));
return style;
}