zhangback
2026-03-10 073d2fa1d86fcb998bc27e79cdc3773da7ed8b2b
common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java
@@ -4,7 +4,6 @@
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.*;
@@ -18,6 +17,7 @@
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;
@@ -39,7 +39,6 @@
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
 * Excel 处理工具类 - 支持分页导出和大批量数据处理
@@ -58,7 +57,7 @@
    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; // 当前工作表
@@ -257,6 +256,32 @@
            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;
    }
    /**
@@ -1045,7 +1070,8 @@
     */
    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;
    }
    /**
@@ -1244,7 +1270,7 @@
     * 转换单元格值为目标类型
     */
    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 {
@@ -1351,6 +1377,10 @@
     * @return 字典值
     */
    public static String reverseDictByExp(String dictLabel, String dictType, String separator) {
        if (StringUtils.isEmpty(dictLabel))
        {
            return StringUtils.EMPTY;
        }
        return DictUtils.getDictValue(dictType, dictLabel, separator);
    }
}