如何为合并单元格 Apache poi 添加边框?

问题描述 投票:0回答:1

`我合并了单元格 这是它们的样子,有 22 个,我还有 2 个 css cs 样式,现在需要为合并的单元格添加边框,我尝试添加边框,但随后文本样式更新了

cellRangeAddress = new CellRangeAddress(1, 1, 4, 8);

public CellStyle mergedStyle(CellStyle textStyle, Sheet sheet,Workbook workbook){ CellStyle styleBorder = workbook.createCellStyle(); styleBorder.cloneStyleFrom(textStyle); styleBorder.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); styleBorder.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM); styleBorder.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); styleBorder.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);

//there are 13 here because 13 columns is correct    
for (int rowIndex = 0; rowIndex <= 13; rowIndex++) {
        Row row1 = sheet.getRow(rowIndex);
        if (row1 == null) {
            row1 = sheet.createRow(rowIndex);  
        }
        for (int columnIndex = 0; columnIndex <= 13; columnIndex++) {
            Cell cell1 = row1.getCell(columnIndex); 
            if (cell1 == null) {
                cell1 = row1.createCell(columnIndex);       
            }
            cell1.setCellStyle(styleBorder);    
        }
    }
    return styleBorder;    }
CellStyle cellStyleCss = mergedStyle(css, sheet, workbook); CellStyle cellStyleCs = mergedStyle(cs, sheet, workbook);

使用这种用法,只有部分显示有边框`

java excel apache apache-poi
1个回答
0
投票

考虑使用 CSS 框架或样板来简化代码并使其更加一致。

您可以使用 CSS 网格或 Flexbox 来实现此布局。这两种方法都允许您创建元素的行和列并控制它们的间距和对齐方式。网上有许多资源可以帮助您学习如何有效地使用这些技术。

编辑:同样在循环中,无需调用 mergedStyle,只需将 borderStyle 分配给单元格,这样所有单元格都将应用相同的边框样式。

© www.soinside.com 2019 - 2024. All rights reserved.