[alibaba/easyexcel]Excel填充list合并单元格边框线格式不能正确生成

2023-12-14 246 views
0

image @Override protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) { if (relativeRowIndex == null || relativeRowIndex == 0) { return; } int rowIndex = cell.getRowIndex(); int colIndex = cell.getColumnIndex(); sheet = cell.getSheet(); Row preRow = sheet.getRow(rowIndex - 1); //获取上一行的该格 Cell preCell = preRow.getCell(colIndex); List<CellRangeAddress> list = sheet.getMergedRegions(); CellStyle cellStyle = preCell.getCellStyle(); cell.setCellStyle(cellStyle); for (CellRangeAddress cellRangeAddress : list) { if (cellRangeAddress.containsRow(preCell.getRowIndex()) && cellRangeAddress.containsColumn(preCell.getColumnIndex())) { int lastColIndex = cellRangeAddress.getLastColumn(); int firstColIndex = cellRangeAddress.getFirstColumn(); CellRangeAddress cra = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(), firstColIndex, lastColIndex); sheet.addMergedRegion(cra); if (preCell.getStringCellValue().equalsIgnoreCase("2023-01-16")) { String stringCellValue = preCell.getStringCellValue(); } RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cra, sheet); RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cra, sheet); RegionUtil.setBorderRight(cellStyle.getBorderRight(), cra, sheet); RegionUtil.setBorderTop(cellStyle.getBorderTop(), cra, sheet); return; } } }

回答

8

上面的代码有点问题 @Override protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) { if (relativeRowIndex == null || relativeRowIndex == 0) { return; } int rowIndex = cell.getRowIndex(); int colIndex = cell.getColumnIndex(); sheet = cell.getSheet(); Row preRow = sheet.getRow(rowIndex - 1); //获取上一行的该格 Cell preCell = preRow.getCell(colIndex); List<CellRangeAddress> list = sheet.getMergedRegions(); CellStyle cellStyle = preCell.getCellStyle(); cell.setCellStyle(cellStyle); for (CellRangeAddress cellRangeAddress : list) { if (cellRangeAddress.containsRow(preCell.getRowIndex()) && cellRangeAddress.containsColumn(preCell.getColumnIndex())) { int lastColIndex = cellRangeAddress.getLastColumn(); int firstColIndex = cellRangeAddress.getFirstColumn(); CellRangeAddress cra = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(), firstColIndex, lastColIndex); sheet.addMergedRegion(cra); RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cra, sheet); RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cra, sheet); RegionUtil.setBorderRight(cellStyle.getBorderRight(), cra, sheet); RegionUtil.setBorderTop(cellStyle.getBorderTop(), cra, sheet); return; } } }

2

具体是哪个符合期望?

2

合并单元格框线颜色,部分和模板中的不一样 当前为: image 期望:合并单元格填充list后后面每一行都和第一行一样的框线样式 image 我填充的模板为下图: image

2

试试看这段代码

// MergeStrategy
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        if (relativeRowIndex == null || relativeRowIndex == 0) {
            return;
        }
        int rowIndex = cell.getRowIndex();
        int colIndex = cell.getColumnIndex();
        Sheet thisSheet = cell.getSheet();
        Row preRow = thisSheet.getRow(rowIndex - 1);
        Row thisRow = thisSheet.getRow(rowIndex);
        Cell preCell = preRow.getCell(colIndex);// 获取上一行的该格
        Cell tmpCell;
        List<CellRangeAddress> list = thisSheet.getMergedRegions();

        for (int i = 0; i < list.size(); i++) {
            CellRangeAddress cellRangeAddress = list.get(i);
            if (cellRangeAddress.containsRow(preCell.getRowIndex()) && cellRangeAddress.containsColumn(preCell.getColumnIndex())) {
                int lastColIndex = cellRangeAddress.getLastColumn();
                int firstColIndex = cellRangeAddress.getFirstColumn();
                CellRangeAddress cra = new CellRangeAddress(cell.getRowIndex(), cell.getRowIndex(), firstColIndex, lastColIndex);
                thisSheet.addMergedRegion(cra);
                for (int j = firstColIndex; j <= lastColIndex; j++) {
                    tmpCell = thisRow.getCell(j);
                    if (tmpCell == null) {
                        tmpCell = thisRow.createCell(j);
                    }
                    tmpCell.setCellStyle(preRow.getCell(j).getCellStyle());
                }
                return;
            }
        }
    }
0

多谢,完美解决了, 我自己只能解决到这样了 image