tanghaihao
1 year ago
5 changed files with 313 additions and 9 deletions
@ -0,0 +1,34 @@
|
||||
package com.hnac.hzims.operational.duty.entity; |
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty; |
||||
import com.alibaba.excel.annotation.write.style.ColumnWidth; |
||||
import com.alibaba.excel.annotation.write.style.ContentLoopMerge; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author tanghaihao |
||||
* @date 2023年06月09日 10:56 |
||||
*/ |
||||
@Data |
||||
public class ImsDutyMainReportExcel { |
||||
@ExcelProperty(value = "排班日期", index = 0) |
||||
@ColumnWidth(value = 20) |
||||
@ApiModelProperty(value = "排班日期") |
||||
private String dutyDate; |
||||
|
||||
@ExcelProperty(value = "班次", index = 1) |
||||
@ColumnWidth(value = 30) |
||||
@ApiModelProperty(value = "班次") |
||||
private String className; |
||||
|
||||
@ExcelProperty(value = "班组长", index = 2) |
||||
@ColumnWidth(value = 20) |
||||
@ApiModelProperty(value = "班组长") |
||||
private String managerName; |
||||
|
||||
@ExcelProperty(value = "组员", index = 3) |
||||
@ColumnWidth(value = 100) |
||||
@ApiModelProperty(value = "组员") |
||||
private String personNames; |
||||
} |
@ -0,0 +1,110 @@
|
||||
package com.hnac.hzims.operational.duty.utils; |
||||
|
||||
import com.alibaba.excel.metadata.CellData; |
||||
import com.alibaba.excel.metadata.Head; |
||||
import com.alibaba.excel.write.handler.CellWriteHandler; |
||||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
||||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
||||
import org.apache.poi.ss.usermodel.Cell; |
||||
import org.apache.poi.ss.usermodel.CellType; |
||||
import org.apache.poi.ss.usermodel.Row; |
||||
import org.apache.poi.ss.usermodel.Sheet; |
||||
import org.apache.poi.ss.util.CellRangeAddress; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 合并相同内容的单元格处理类 |
||||
* @author tanghaihao |
||||
* @date 2023年06月12日 16:43 |
||||
*/ |
||||
public class ExcelMergeHandler implements CellWriteHandler { |
||||
private int[] mergeColumnIndex; |
||||
private int mergeRowIndex; |
||||
|
||||
public ExcelMergeHandler() { |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param mergeRowIndex 从第几行后开始合并,行数从1开始 |
||||
* @param mergeColumnIndex 需要合并的列,数组类型可以选择多列,列数从0开始 |
||||
*/ |
||||
public ExcelMergeHandler(int mergeRowIndex, int[] mergeColumnIndex) { |
||||
this.mergeRowIndex = mergeRowIndex; |
||||
this.mergeColumnIndex = mergeColumnIndex; |
||||
} |
||||
|
||||
@Override |
||||
public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) { |
||||
|
||||
//当前行
|
||||
int curRowIndex = cell.getRowIndex(); |
||||
//当前列
|
||||
int curColIndex = cell.getColumnIndex(); |
||||
|
||||
if (curRowIndex > mergeRowIndex) { |
||||
for (int i = 0; i < mergeColumnIndex.length; i++) { |
||||
if (curColIndex == mergeColumnIndex[i]) { |
||||
mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex); |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 当前单元格向上合并 |
||||
* |
||||
* @param writeSheetHolder |
||||
* @param cell 当前单元格 |
||||
* @param curRowIndex 当前行 |
||||
* @param curColIndex 当前列 |
||||
*/ |
||||
private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) { |
||||
Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue(); |
||||
Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex); |
||||
Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue(); |
||||
// 将当前单元格数据与上一个单元格数据比较
|
||||
Boolean dataBool = preData.equals(curData); |
||||
String s1 = cell.getRow().getCell(0).getStringCellValue(); |
||||
String s2 = cell.getSheet().getRow(curRowIndex - 1).getCell(0).getStringCellValue(); |
||||
Boolean bool = s1.compareTo(s2) == 0 ? true:false; |
||||
if (dataBool && bool) { |
||||
Sheet sheet = writeSheetHolder.getSheet(); |
||||
List<CellRangeAddress> mergeRegions = sheet.getMergedRegions(); |
||||
boolean isMerged = false; |
||||
for (int i = 0; i < mergeRegions.size() && !isMerged; i++) { |
||||
CellRangeAddress cellRangeAddr = mergeRegions.get(i); |
||||
// 若上一个单元格已经被合并,则先移出原有的合并单元,再重新添加合并单元
|
||||
if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) { |
||||
sheet.removeMergedRegion(i); |
||||
cellRangeAddr.setLastRow(curRowIndex); |
||||
sheet.addMergedRegion(cellRangeAddr); |
||||
isMerged = true; |
||||
} |
||||
} |
||||
// 若上一个单元格未被合并,则新增合并单元
|
||||
if (!isMerged) { |
||||
CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex); |
||||
sheet.addMergedRegion(cellRangeAddress); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue