1804 changed files with 11860 additions and 4662 deletions
@ -0,0 +1,43 @@ |
|||
package com.epmet.commons.tools.enums; |
|||
|
|||
/** |
|||
* 用户详细状态:01:新增、02:导入、03:迁入、04:新生、11:迁出、21死亡 未完待续 |
|||
*/ |
|||
public enum IcResiUserSubStatusEnum { |
|||
/** |
|||
* 环境变量枚举 |
|||
*/ |
|||
ADD("01", "新增"), |
|||
IMPORT("02", "导入"), |
|||
MOVE_IN("03", "迁入"), |
|||
BIRTH("04", "新生"), |
|||
MOVE_OUT("11", "迁出"), |
|||
DIED("21", "死亡"); |
|||
|
|||
private final String subStatus; |
|||
private final String statusDesc; |
|||
|
|||
|
|||
IcResiUserSubStatusEnum(String subStatus, String statusDesc) { |
|||
this.subStatus = subStatus; |
|||
this.statusDesc = statusDesc; |
|||
} |
|||
|
|||
public static IcResiUserSubStatusEnum getEnum(String subStatus) { |
|||
IcResiUserSubStatusEnum[] values = IcResiUserSubStatusEnum.values(); |
|||
for (IcResiUserSubStatusEnum value : values) { |
|||
if (value.getSubStatus().equals(subStatus)) { |
|||
return value; |
|||
} |
|||
} |
|||
return IcResiUserSubStatusEnum.ADD; |
|||
} |
|||
|
|||
public String getSubStatus() { |
|||
return subStatus; |
|||
} |
|||
|
|||
public String getStatusDesc() { |
|||
return statusDesc; |
|||
} |
|||
} |
@ -0,0 +1,162 @@ |
|||
package com.epmet.commons.tools.utils.poi.excel.handler; |
|||
|
|||
import com.alibaba.excel.write.handler.RowWriteHandler; |
|||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
|||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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 org.apache.poi.xssf.usermodel.XSSFSheet; |
|||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCell; |
|||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTMergeCells; |
|||
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; |
|||
|
|||
import java.lang.reflect.Field; |
|||
|
|||
/** |
|||
* desc:按行对单元格合并策略 依据是第一列的值 |
|||
* |
|||
* @author liujianjun |
|||
*/ |
|||
@Slf4j |
|||
public class ExcelFillRowMergeStrategy implements RowWriteHandler { |
|||
|
|||
/** |
|||
* 分段总数据量 本批次写入的数据总量 |
|||
*/ |
|||
private Integer secTotalCount; |
|||
|
|||
/** |
|||
* 需要合并的列 下标 |
|||
*/ |
|||
private int[] mergeColumnIndexArr; |
|||
|
|||
/** |
|||
* 默认隐藏第一列 用于合并数据 |
|||
*/ |
|||
private boolean hiddenFirst = true; |
|||
|
|||
//已合并单元格数
|
|||
private int mergedTotalCount = 0; |
|||
|
|||
//合并行计数
|
|||
private int count; |
|||
|
|||
|
|||
|
|||
public ExcelFillRowMergeStrategy(int[] mergeColumnIndexArr) { |
|||
this.mergeColumnIndexArr = mergeColumnIndexArr; |
|||
} |
|||
|
|||
public ExcelFillRowMergeStrategy(int[] mergeColumnIndexArr, boolean hiddenFirst) { |
|||
this.mergeColumnIndexArr = mergeColumnIndexArr; |
|||
this.hiddenFirst = hiddenFirst; |
|||
} |
|||
|
|||
@Override |
|||
public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) { |
|||
// 隐藏id列
|
|||
if (hiddenFirst) { |
|||
writeSheetHolder.getSheet().setColumnHidden(0, true); |
|||
} |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) { |
|||
//当前行索引
|
|||
int curRowNum = row.getRowNum(); |
|||
if (mergeColumnIndexArr != null && mergeColumnIndexArr.length > 0 && !isHead) { |
|||
//当前行第一列单元格
|
|||
Cell curA1Cell = row.getCell(0); |
|||
Object curA1Data = curA1Cell.getCellType() == CellType.STRING ? curA1Cell.getStringCellValue() : curA1Cell.getNumericCellValue(); |
|||
//上一行第一列单元格
|
|||
Cell preA1Cell = row.getSheet().getRow(curRowNum - 1).getCell(0); |
|||
Object preA1Data = preA1Cell.getCellType() == CellType.STRING ? preA1Cell.getStringCellValue() : preA1Cell.getNumericCellValue(); |
|||
|
|||
log.info("curRowNum:{},是否相等:{}",curRowNum,curA1Data.equals(preA1Data)); |
|||
|
|||
if (curA1Data.equals(preA1Data)){ |
|||
count ++; |
|||
log.info("需要合并的列数:{}",count); |
|||
}else { |
|||
if (count > 0){ |
|||
log.info("需要合并了"); |
|||
for (int i = 0; i < mergeColumnIndexArr.length; i++) { |
|||
mergeSomeRow(writeSheetHolder,curRowNum,count,i); |
|||
} |
|||
count = 0; |
|||
} |
|||
} |
|||
|
|||
if (curRowNum == secTotalCount && count > 0){ |
|||
for (int i = 0; i < mergeColumnIndexArr.length; i++) { |
|||
mergeSomeRow(writeSheetHolder,curRowNum + 1,count,i); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
/*private void mergeSameRow(WriteSheetHolder writeSheetHolder, int curRowIndex, int curColIndex) { |
|||
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.addMergedRegionUnsafe(cellRangeAddr); |
|||
isMerged = true; |
|||
} |
|||
} |
|||
// 若上一个单元格未被合并,则新增合并单元
|
|||
if (!isMerged) { |
|||
CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex); |
|||
sheet.addMergedRegionUnsafe(cellRangeAddress); |
|||
} |
|||
} |
|||
*/ |
|||
|
|||
|
|||
/** |
|||
* 按列合并单元格 |
|||
* @param writeSheetHolder |
|||
* @param curRowIndex 当前行索引,有n行固定行就加n |
|||
* @param needMergeNum 需要合并的行数 |
|||
* @param curColIndex 需要合并的列 |
|||
*/ |
|||
private void mergeSomeRow(WriteSheetHolder writeSheetHolder, int curRowIndex, int needMergeNum, int curColIndex) { |
|||
Sheet sheet = writeSheetHolder.getSheet(); |
|||
try { |
|||
CellRangeAddress cellAddresses = new CellRangeAddress(curRowIndex - needMergeNum - 1, curRowIndex-1, curColIndex, curColIndex); |
|||
Field sh = sheet.getClass().getDeclaredField("_sh"); |
|||
sh.setAccessible(true); |
|||
XSSFSheet shSheet = (XSSFSheet)sh.get(sheet); |
|||
CTWorksheet worksheet = shSheet.getCTWorksheet(); |
|||
CTMergeCells ctMergeCells = mergedTotalCount > 0 ? worksheet.getMergeCells() : worksheet.addNewMergeCells(); |
|||
CTMergeCell ctMergeCell = ctMergeCells.addNewMergeCell(); |
|||
ctMergeCell.setRef(cellAddresses.formatAsString()); |
|||
mergedTotalCount ++; |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
public void setSecTotalCount(Integer secTotalCount) { |
|||
this.secTotalCount = secTotalCount; |
|||
} |
|||
} |
@ -0,0 +1,105 @@ |
|||
package com.epmet.commons.tools.utils.poi.excel.handler; |
|||
|
|||
import com.alibaba.excel.write.handler.RowWriteHandler; |
|||
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder; |
|||
import com.alibaba.excel.write.metadata.holder.WriteTableHolder; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
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; |
|||
|
|||
/** |
|||
* desc:按行对单元格合并策略 依据是第一列的值 |
|||
* |
|||
* @author liujianjun |
|||
*/ |
|||
@Slf4j |
|||
public class ExcelFillRowMergeStrategy2 implements RowWriteHandler { |
|||
|
|||
/** |
|||
* 需要合并的列 下标 |
|||
*/ |
|||
private int[] mergeColumnIndexArr; |
|||
/** |
|||
* 从下标n行开始合并 |
|||
*/ |
|||
private int mergeRowIndex; |
|||
/** |
|||
* 默认隐藏第一列 用于合并数据 |
|||
*/ |
|||
private boolean hiddenFirst = true; |
|||
|
|||
|
|||
public ExcelFillRowMergeStrategy2(int mergeRowIndex, int[] mergeColumnIndexArr) { |
|||
this.mergeRowIndex = mergeRowIndex; |
|||
this.mergeColumnIndexArr = mergeColumnIndexArr; |
|||
} |
|||
|
|||
public ExcelFillRowMergeStrategy2(int mergeRowIndex, int[] mergeColumnIndexArr, boolean hiddenFirst) { |
|||
this.mergeRowIndex = mergeRowIndex; |
|||
this.mergeColumnIndexArr = mergeColumnIndexArr; |
|||
this.hiddenFirst = hiddenFirst; |
|||
} |
|||
|
|||
@Override |
|||
public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer rowIndex, Integer relativeRowIndex, Boolean isHead) { |
|||
|
|||
} |
|||
|
|||
@Override |
|||
public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) { |
|||
// 隐藏id列
|
|||
if (hiddenFirst) { |
|||
writeSheetHolder.getSheet().setColumnHidden(0, true); |
|||
} |
|||
} |
|||
|
|||
|
|||
@Override |
|||
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) { |
|||
//当前行索引
|
|||
int curRowNum = row.getRowNum(); |
|||
if (mergeColumnIndexArr != null && mergeColumnIndexArr.length > 0 && !isHead) { |
|||
//当前行第一列单元格
|
|||
Cell curA1Cell = row.getCell(0); |
|||
Object curA1Data = curA1Cell.getCellType() == CellType.STRING ? curA1Cell.getStringCellValue() : curA1Cell.getNumericCellValue(); |
|||
//上一行第一列单元格
|
|||
Cell preA1Cell = row.getSheet().getRow(curRowNum - 1).getCell(0); |
|||
Object preA1Data = preA1Cell.getCellType() == CellType.STRING ? preA1Cell.getStringCellValue() : preA1Cell.getNumericCellValue(); |
|||
|
|||
if (curA1Data.equals(preA1Data)) { |
|||
for (int value : mergeColumnIndexArr) { |
|||
mergeSameRow(writeSheetHolder, curRowNum, value); |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
|
|||
|
|||
private void mergeSameRow(WriteSheetHolder writeSheetHolder, int curRowIndex, int curColIndex) { |
|||
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.addMergedRegionUnsafe(cellRangeAddr); |
|||
isMerged = true; |
|||
} |
|||
} |
|||
// 若上一个单元格未被合并,则新增合并单元
|
|||
if (!isMerged) { |
|||
CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex); |
|||
sheet.addMergedRegionUnsafe(cellRangeAddress); |
|||
} |
|||
} |
|||
|
|||
} |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue