| | |
| | | import com.ruoyi.common.annotation.Excel.ColumnType; |
| | | import com.ruoyi.common.annotation.Excels; |
| | | import com.ruoyi.common.config.RuoYiConfig; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.text.Convert; |
| | | import com.ruoyi.common.exception.UtilException; |
| | | import com.ruoyi.common.utils.*; |
| | |
| | | import org.apache.poi.ss.usermodel.*; |
| | | import org.apache.poi.ss.util.CellRangeAddress; |
| | | import org.apache.poi.ss.util.CellRangeAddressList; |
| | | import org.apache.poi.xssf.streaming.SXSSFSheet; |
| | | import org.apache.poi.xssf.streaming.SXSSFWorkbook; |
| | | import org.apache.poi.xssf.usermodel.XSSFClientAnchor; |
| | | import org.apache.poi.xssf.usermodel.XSSFDataValidation; |
| | |
| | | import java.util.*; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.function.Predicate; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * Excel 处理工具类 - 支持分页导出和大批量数据处理 |
| | |
| | | private static final String DEFAULT_FONT = "Arial" ; // 默认字体 |
| | | |
| | | // 核心属性 |
| | | private final Class<T> clazz; // 实体类类型 |
| | | private Class<T> clazz; // 实体类类型 |
| | | private SXSSFWorkbook workbook; // Excel 工作簿对象 |
| | | private Workbook wb; // Excel 工作簿对象 |
| | | private Sheet currentSheet; // 当前工作表 |
| | |
| | | if (hasSubList()) createSubHeader(); |
| | | writeHeaderRow(); |
| | | } |
| | | } |
| | | public ExcelUtil switchSheet(int index, String name, Class clazz){ |
| | | sheetIndex = index; |
| | | sheetName = name; |
| | | this.clazz = Objects.requireNonNull(clazz, "Class type cannot be null"); |
| | | |
| | | SXSSFSheet sheetAt = null; |
| | | // 检查索引是否在有效范围内 |
| | | if (index >= 0 && index < workbook.getNumberOfSheets()) { |
| | | try { |
| | | sheetAt = workbook.getSheetAt(index); |
| | | } catch (IllegalArgumentException e) { |
| | | // 如果仍然有问题,跳过并创建新的 |
| | | } |
| | | } |
| | | if (sheetAt == null){ |
| | | initializeFields(); |
| | | currentSheet = workbook.createSheet(sheetName); |
| | | currentRowNum.set(0); |
| | | createTitleRow(); |
| | | if (hasSubList()) createSubHeader(); |
| | | writeHeaderRow(); |
| | | }else{ |
| | | currentSheet = sheetAt; |
| | | } |
| | | return this; |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | private String getDictLabel(String value, String dictType, String separator) { |
| | | String key = dictType + value; |
| | | return dictCache.computeIfAbsent(key, k -> DictUtils.getDictLabel(dictType, value, separator)); |
| | | String s = dictCache.computeIfAbsent(key, k -> DictUtils.getDictLabel(dictType, value, separator)); |
| | | return StringUtils.isEmpty(s)?value:s; |
| | | } |
| | | |
| | | /** |
| | |
| | | * 转换单元格值为目标类型 |
| | | */ |
| | | private Object convertValue(Object val, Class<?> fieldType, Excel attr) { |
| | | String strVal = Convert.toStr(val).trim(); |
| | | String strVal = Convert.toStr(val); |
| | | if (StringUtils.isEmpty(strVal)) return null; |
| | | |
| | | try { |
| | |
| | | * @return 字典值 |
| | | */ |
| | | public static String reverseDictByExp(String dictLabel, String dictType, String separator) { |
| | | if (StringUtils.isEmpty(dictLabel)) |
| | | { |
| | | return StringUtils.EMPTY; |
| | | } |
| | | return DictUtils.getDictValue(dictType, dictLabel, separator); |
| | | } |
| | | } |