laowei93 commented on issue #721:
URL: https://github.com/apache/fesod/issues/721#issuecomment-3604763941
List<CellRangeAddress> cellRangeAddressesList =
getOthrCellRangeAddress(list, 4);
WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new
CustomMergeStrategyTest(ConfirmationClassRecordExportVO.class,
cellRangeAddressesList))
.head(ConfirmationClassRecordExportVO.class)
.build();
try (ExcelWriter excelWriter =
EasyExcel.write(response.getOutputStream(),
ConfirmationClassRecordExportVO.class).build()) {
excelWriter.write(list, writeSheet);
}
private List<CellRangeAddress>
getOthrCellRangeAddress(List<ConfirmationClassRecordExportVO> othr, int maxCol)
{
ArrayList<CellRangeAddress> cellRangeAddresses = new ArrayList<>();
//'护照'合并
int passportNumFirstRow = 1;
int passportNumLastRow = 1;
for (int i = 0; i < othr.size() - 1; i++) {
if (othr.get(i).getConfirmationClassRecordId().equals(othr.get(i
+ 1).getConfirmationClassRecordId())) {
//可以合并
passportNumLastRow++;
} else {
if (passportNumLastRow > passportNumFirstRow) {
for (int j = 0; j <= maxCol; j++) {
CellRangeAddress cellRangeAddress = new
CellRangeAddress(passportNumFirstRow,
passportNumLastRow, j, j);
cellRangeAddresses.add(cellRangeAddress);
}
}
passportNumFirstRow = passportNumLastRow + 1;
passportNumLastRow = passportNumFirstRow;
}
if (i == othr.size() - 2) {
if (passportNumLastRow > passportNumFirstRow) {
for (int j = 0; j <= maxCol; j++) {
CellRangeAddress cellRangeAddress = new
CellRangeAddress(passportNumFirstRow,
passportNumLastRow, j, j);
cellRangeAddresses.add(cellRangeAddress);
}
}
}
}
return cellRangeAddresses;
}
public class CustomMergeStrategyTest implements SheetWriteHandler {
/**
* DTO数据类型
*/
private final Class<?> elementType;
// 增加自定义合并key如果自定义合并key为空按照原有策略走
private List<CellRangeAddress> cellRangeAddressesList;
public CustomMergeStrategyTest(Class<?> elementType,
List<CellRangeAddress> cellRangeAddressesList) {
this.elementType = elementType;
this.cellRangeAddressesList = cellRangeAddressesList;
}
@Override
public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder,
WriteSheetHolder writeSheetHolder) {
// 所有数据写入完毕,开始批量合并
Sheet sheet = writeSheetHolder.getSheet();
if (CollectionUtils.isNotEmpty(cellRangeAddressesList)) {
long startTime = System.currentTimeMillis();
for (CellRangeAddress cellRangeAddress : cellRangeAddressesList)
{
sheet.addMergedRegionUnsafe(cellRangeAddress);
}
long endTime = System.currentTimeMillis();
System.out.println("合并完成,耗时:" + (endTime - startTime) + " 毫秒");
}
}
}
[20251203102025.xlsx](https://github.com/user-attachments/files/23894289/20251203102025.xlsx)
[20251203102025.xlsx](https://github.com/user-attachments/files/23894290/20251203102025.xlsx)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]