package com.ai.hdl.market.infrastructure.utils.easyexcel;import com.ai.hdl.market.infrastructure.utils.AppContextUtil;import com.alibaba.excel.annotation.ExcelProperty;import com.alibaba.excel.write.handler.SheetWriteHandler;import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;import org.apache.commons.lang.StringUtils;import org.apache.poi.ss.usermodel.*;import org.apache.poi.ss.util.CellRangeAddressList;import org.apache.poi.xssf.usermodel.XSSFDataValidation;import org.hibernate.validator.constraints.Length;import javax.validation.constraints.Max;import javax.validation.constraints.Min;import java.lang.reflect.Field;import java.math.BigDecimal;import java.util.Arrays;import java.util.List;public class ExplicitWriteHandler implements SheetWriteHandler { private static Class<?> clazz; private int lineNum; private final List<Class<?>> numClassList = Arrays.asList(Float.class, Double.class, BigDecimal.class, Integer.class, Short.class, Byte.class); public ExplicitWriteHandler(Class<?> clazz) { ExplicitWriteHandler.clazz = clazz; this.lineNum = 1000; } public void lineNum(int num) { this.lineNum = num; } @Override public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { } @Override public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { Sheet sheet = writeSheetHolder.getSheet(); sheet.createFreezePane(0, 1, 0, 1); DataValidationHelper helper = sheet.getDataValidationHelper(); Field[] declaredFields = clazz.getDeclaredFields(); for (int i = 0; i < declaredFields.length; i++) { Field field = declaredFields[i]; int columnIndex = i; //解析注解信息 ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class); if (excelProperty != null) { columnIndex = excelProperty.index(); } ExplicitConstraint explicitConstraint = field.getAnnotation(ExplicitConstraint.class); String[] explicitArray = resolveExplicitConstraint(explicitConstraint); if (explicitArray != null && explicitArray.length > 0) { explicitFormat(columnIndex, explicitArray, sheet, helper); } ExcelDateFormat format = field.getAnnotation(ExcelDateFormat.class); if (format != null) { dateCellFormat(columnIndex, format.format(), sheet, helper); } Class<?> fieldType = field.getType(); if (numClassList.contains(fieldType)) { numberCellValid(columnIndex, field, fieldType, sheet, helper); } if (fieldType.equals(String.class)) { textCellStyle(columnIndex, sheet); } Length length = field.getAnnotation(Length.class); if (length != null) { textLengthValid(columnIndex, length.min(), length.max(), sheet, helper); } } } private void textCellStyle(Integer columnIndex, Sheet sheet) { Workbook book = sheet.getWorkbook(); CellStyle cellStyle = book.createCellStyle(); DataFormat dataFormat = book.createDataFormat(); cellStyle.setDataFormat(dataFormat.getFormat("@")); cellStyle.setWrapText(false); sheet.setDefaultColumnStyle(columnIndex, cellStyle); } private void textLengthValid(Integer columnIndex, Integer min, Integer max, Sheet sheet, DataValidationHelper helper) { CellRangeAddressList addressList = new CellRangeAddressList(1, lineNum, columnIndex, columnIndex); int opType = DataValidationConstraint.OperatorType.BETWEEN; DataValidationConstraint constraint = helper.createTextLengthConstraint(opType, Integer.toString(min), Integer.toString(max)); DataValidation dataValidation = helper.createValidation(constraint, addressList); //处理Excel兼容性问题 setDataValidation(dataValidation, "数据长度超出限制[" + min + "~" + max + "]"); sheet.addValidationData(dataValidation); } private void explicitFormat(Integer columnIndex, String[] sources, Sheet sheet, DataValidationHelper helper) { // 起始行、终止行、起始列、终止列 CellRangeAddressList addressList = new CellRangeAddressList(1, lineNum, columnIndex, columnIndex); //设置下拉框数据 DataValidationConstraint constraint = helper.createExplicitListConstraint(sources); DataValidation dataValidation = helper.createValidation(constraint, addressList); //处理Excel兼容性问题 setDataValidation(dataValidation, null); sheet.addValidationData(dataValidation); } private void numberCellValid(Integer columnIndex, Field field, Class<?> clazz, Sheet sheet, DataValidationHelper helper) { int opType = DataValidationConstraint.OperatorType.BETWEEN; int vType = DataValidationConstraint.ValidationType.INTEGER; if (clazz.equals(Float.class) || clazz.equals(Double.class) || clazz.equals(BigDecimal.class)) { vType = DataValidationConstraint.ValidationType.DECIMAL; } Min minAnnotation = field.getAnnotation(Min.class); Max maxAnnotation = field.getAnnotation(Max.class); String min = minAnnotation == null ? "0" : Long.toString(minAnnotation.value()); String max = maxAnnotation == null ? "99999999" : Long.toString(maxAnnotation.value()); CellRangeAddressList addressList = new CellRangeAddressList(1, lineNum, columnIndex, columnIndex); DataValidationConstraint constraint = helper.createNumericConstraint(vType, opType, min, max); DataValidation dataValidation = helper.createValidation(constraint, addressList); setDataValidation(dataValidation, "请输入正确的数值"); sheet.addValidationData(dataValidation); } private void dateCellFormat(Integer columnIndex, String format, Sheet sheet, DataValidationHelper helper) { Workbook book = sheet.getWorkbook(); CellStyle cellStyle = book.createCellStyle(); short dateFmt = book.createDataFormat().getFormat(format); cellStyle.setDataFormat(dateFmt); sheet.setDefaultColumnStyle(columnIndex, cellStyle); int opType = DataValidationConstraint.OperatorType.BETWEEN; String minDate = "Date(1900, 1, 1)"; String maxDate = "Date(3000, 1, 1)"; CellRangeAddressList addressList = new CellRangeAddressList(1, lineNum, columnIndex, columnIndex); DataValidationConstraint constraint = helper.createDateConstraint(opType, minDate, maxDate, format); DataValidation dataValidation = helper.createValidation(constraint, addressList); setDataValidation(dataValidation, "请输入[" + format + "]格式日期"); sheet.addValidationData(dataValidation); } private void setDataValidation(DataValidation dataValidation, String errorMsg) { if (dataValidation instanceof XSSFDataValidation) { dataValidation.setSuppressDropDownArrow(true); dataValidation.setShowErrorBox(true); if (StringUtils.isNotBlank(errorMsg)) { dataValidation.createErrorBox("温馨提示", errorMsg); } } else { dataValidation.setSuppressDropDownArrow(false); if (StringUtils.isNotBlank(errorMsg)) { dataValidation.createPromptBox("温馨提示", errorMsg); } } } private String[] resolveExplicitConstraint(ExplicitConstraint explicitConstraint) { if (explicitConstraint == null) { return null; } //固定下拉信息 String[] source = explicitConstraint.source(); if (source.length <= 0) { //动态下拉信息 @SuppressWarnings("unchecked") Class<? extends ExplicitConstraint.IExplicitInterface>[] classes = explicitConstraint.sourceClass(); if (classes.length > 0) { ExplicitConstraint.IExplicitInterface explicitInterface = AppContextUtil.getBean(classes[0]); source = explicitInterface.source(); } } if (source == null || source.length <= 0) { source = null; } return source; }}