jinlin
1 天以前 bf5b01b14dc7bfc214e646425a62f5593890d7e3
src/main/java/com/example/server/utils/ImportUtil.java
@@ -1,14 +1,20 @@
package com.example.server.utils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;
import static org.apache.commons.lang3.ObjectUtils.isEmpty;
public class ImportUtil {
@@ -77,4 +83,188 @@
        }
        return cellStr;
    }
    /**
     * 判断时间格式是否正确
     *
     * @param value
     * @param format
     * @return
     */
    public static boolean isDate(String value, String format) {
        SimpleDateFormat sdf = null;
        ParsePosition pos = new ParsePosition(0);//指定从所传字符串的首位开始解析
        if (value == null || isEmpty(format)) {
            return false;
        }
        try {
            sdf = new SimpleDateFormat(format);
            sdf.setLenient(false);
            Date date = sdf.parse(value, pos);
            if (date == null) {
                return false;
            } else {
                //更为严谨的日期,如2011-03-024认为是不合法的
                if (pos.getIndex() > sdf.format(date).length()) {
                    return false;
                }
                return true;
            }
        } catch (Exception e) {
            return false;
        }
    }
    /**
     * 导入异常处理
     * @param errMap
     * @param errSting
     * @param sheetName
     * @param row
     */
    public static void updateErrMap(Map<String, Object> errMap, String errSting, String sheetName, int row) {
        if (!errMap.containsKey(errSting)) {
            errMap.put(errSting, new ImportErrMessage(sheetName, row + "、"));
        } else {
            ImportErrMessage importErrMessage1 = (ImportErrMessage) errMap.get(errSting);
            importErrMessage1.setLineNumber(importErrMessage1.getLineNumber() + row + "、");
        }
    }
    /**
     * 从Excel的日期字段导入数据
     *  1. 如果Excel单元格为空,则返回null
     *  2. 无论Excel单元格格式是什么,只要是日期内容,都能导入,返回Date对象(单元格为文本格式时支持yyyy-mm-dd、yyyy/mm/dd、yyyy.mm.dd及yyyy年MM月dd日格式)
     *  3. 如果Excel单元格内容不是日期,则抛出ExcelImportException
     */
    public static Date getDateFromCell(Cell cell) throws ExcelImportException {
        if (cell == null) return null;
        //定义时间格式
        List<String> DateFormatList = new ArrayList<>();
        DateFormatList.add("yyyy年MM月dd日");
        DateFormatList.add("yyyy/MM/dd");
        DateFormatList.add("yyyy-MM-dd");
        DateFormatList.add("yyyy.MM.dd");
        List<SimpleDateFormat> formatList = new ArrayList<>();
        formatList.add(new SimpleDateFormat("yyyy年MM月dd日"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy/MM/dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy-MM-dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy.MM.dd"));//小写的mm表示的是分钟
        switch (cell.getCellType()) {
            case STRING:
                String strValue = cell.getStringCellValue();
                if (StringUtils.isBlank(strValue)) return null;
                strValue = strValue.trim();
                try {
                    for (int i = 0; i < DateFormatList.size(); i++) {
                        if (ImportUtil.isDate(strValue, DateFormatList.get(i))) {
                            Date date = formatList.get(i).parse(strValue);
                            if (date != null) {
                                return date;
                            }
                        }
                    }
                } catch (ParseException pe) {
                    throw new ExcelImportException("该单元格日期格式不正确");
                }
                throw new ExcelImportException("该单元格日期格式不正确");
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 正常日期格式
                    Date date = cell.getDateCellValue();
                    return date;
                } else {
                    // 检查是否是数字形式的日期
                    double numericValue = cell.getNumericCellValue();
                    if (isLikelyExcelDate(numericValue)) {
                        // 将Excel数字日期转换为Java日期
                        Date date = DateUtil.getJavaDate(numericValue);
                        return date;
                    } else {
                        // 确实是普通数字
                        throw new ExcelImportException("该单元格内容不是日期");
                    }
                }
            default:
                throw new ExcelImportException("该单元格内容不是日期");
        }
    }
    public static String getDateStrFromCell(Cell cell) throws ExcelImportException {
        if (cell == null) return null;
        //定义时间格式
        List<String> DateFormatList = new ArrayList<>();
        DateFormatList.add("yyyy年MM月dd日");
        DateFormatList.add("yyyy/MM/dd");
        DateFormatList.add("yyyy-MM-dd");
        DateFormatList.add("yyyy.MM.dd");
        List<SimpleDateFormat> formatList = new ArrayList<>();
        formatList.add(new SimpleDateFormat("yyyy年MM月dd日"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy/MM/dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy-MM-dd"));//小写的mm表示的是分钟
        formatList.add(new SimpleDateFormat("yyyy.MM.dd"));//小写的mm表示的是分钟
        switch (cell.getCellType()) {
            case STRING:
                String strValue = cell.getStringCellValue();
                if (StringUtils.isBlank(strValue)) return null;
                strValue = strValue.trim();
                try {
                    for (int i = 0; i < DateFormatList.size(); i++) {
                        if (ImportUtil.isDate(strValue, DateFormatList.get(i))) {
                            Date date = formatList.get(i).parse(strValue);
                            if (date != null) {
                                return dateToString(date);
                            }
                        }
                    }
                } catch (ParseException pe) {
                    throw new ExcelImportException("该单元格日期格式不正确");
                }
                throw new ExcelImportException("该单元格日期格式不正确");
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 正常日期格式
                    Date date = cell.getDateCellValue();
                    return dateToString(date);
                } else {
                    // 检查是否是数字形式的日期
                    double numericValue = cell.getNumericCellValue();
                    if (isLikelyExcelDate(numericValue)) {
                        // 将Excel数字日期转换为Java日期
                        Date date = DateUtil.getJavaDate(numericValue);
                        return dateToString(date);
                    } else {
                        // 确实是普通数字
                        throw new ExcelImportException("该单元格内容不是日期");
                    }
                }
            default:
                throw new ExcelImportException("该单元格内容不是日期");
        }
    }
    /**
     * 判断数值是否可能是Excel日期
     * Excel日期通常在一定范围内(如36526-73051对应2000-21000年)
     */
    private static boolean isLikelyExcelDate(double value) {
        // Excel日期从1900-01-01开始,序列号1
        // 现代日期通常在36526-73051范围内
        return value >= 36526 && value < 73051;
    }
    private static String dateToString (Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
}