Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用模板填充时,list列表里面如果有合并单元格的情况,无法正常继承样式 #3770

Open
liu724551711 opened this issue Apr 19, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@liu724551711
Copy link

如图:

image

版本:3.3.2

@liu724551711 liu724551711 added the bug Something isn't working label Apr 19, 2024
@P1an8
Copy link

P1an8 commented Apr 30, 2024

我也遇到了这个问题,不过现在已经解决了需要加个Handler:
EasyExcel.write(file).withTemplate(template).registerWriteHandler(new CellHandler()).build()

public class CellHandler extends AbstractMergeStrategy {

CellStyle cellStyle;

@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 thisSheet = cell.getSheet();
Row preRow = thisSheet.getRow(rowIndex - 1);
Row thisRow = thisSheet.getRow(rowIndex);
Cell preCell = preRow.getCell(colIndex);// 获取上一行的该格
Cell tmpCell;
List 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;
  }
}

}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants