向 POI XSSF 工作簿中的合并区域添加边框 [英] Adding border to a merged region in POI XSSF workbook
问题描述
我使用的是 apache poi 3.7,我需要为一系列单元格或合并区域设置边框.
I'm using apache poi 3.7 and I need to put border to a range of cells or merged region.
当工作表和工作簿类型为 XSSF 时,如何将边框应用于合并区域.在 HSSF 类型中,我使用 RegionUtil-/HSSFRegionutil,但是如果在 XSSF 类型中使用第一个对象 (Regionutil),它就不起作用,并且会为单元格范围添加黑色背景色.
how can I to apply border to a merged region when the sheet and workbook type is XSSF. In HSSF type I use RegionUtil-/HSSFRegionutil, but if use the first object (Regionutil) in XSSF type its doesn't works and puts a black background color to the range of cells.
Regionutil 通常与 CellRangeAddress 一起使用,但我没有找到有关此问题的信息.我不知道 CellRangeAddres 是否会导致这种情况.
Regionutil ussually works with CellRangeAddress and i don't find information about this trouble. I don't know if the CellRangeAddres causes this.
推荐答案
为此,您必须为合并区域中的每个单元格添加一个空白单元格,然后为每个单元格添加适当的边框.例如,以下代码将在同一行中创建一个由 5 个单元格组成的合并区域,整个合并区域周围有边框,文本居中.
In order to do this, you have to add a blank cell to every cell in the merged region, then add the appropriate borders to each cell. For example, the following code will create a merged region of 5 cells in the same row, with a border around the whole merged region, and the text centred in the region.
XSSFWorkbook wb = new XSSFWorkbook();
CellStyle borderStyle = wb.createCellStyle();
borderStyle.setBorderBottom(CellStyle.BORDER_THIN);
borderStyle.setBorderLeft(CellStyle.BORDER_THIN);
borderStyle.setBorderRight(CellStyle.BORDER_THIN);
borderStyle.setBorderTop(CellStyle.BORDER_THIN);
borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
Sheet sheet = wb.createSheet("Test Sheet");
Row row = sheet.createRow(1);
for (int i = 1; i <= 5; ++i) {
Cell cell = row.createCell(i);
cell.setCellStyle(borderStyle);
if (i == 1) {
cell.setCellValue("Centred Text");
}
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5));
这篇关于向 POI XSSF 工作簿中的合并区域添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!