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 {
|
|
/**
|
* 获取当前单元格有几行
|
* @param cell
|
* @param sheet
|
* @return
|
*/
|
public static int getMergeRowNum(Cell cell, Sheet sheet) {
|
int mergeSize = 1;
|
List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
|
for (CellRangeAddress cellRangeAddress : mergedRegions) {
|
if (cellRangeAddress.isInRange(cell)) {
|
mergeSize = cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow() + 1;
|
break;
|
}
|
}
|
return mergeSize;
|
}
|
|
/**
|
* 判断对象是否为空
|
*
|
* @param obj
|
* @return
|
*/
|
public static String ObjectFormat(Object obj) {
|
return obj == null ? "" : obj.toString();
|
}
|
|
// 将所有的值转换为String类型
|
public static String getCellValue(Row row, Integer i, String Pattern) {
|
Cell cell = row.getCell(i);
|
String cellStr = "";
|
cellStr = getCellValue(Pattern, cell, cellStr);
|
return cellStr;
|
}
|
|
public static String getCellValue(String Pattern, Cell cell, String cellStr) {
|
if (cell != null) {
|
switch (cell.getCellType()) {
|
case BLANK:// 空值
|
cellStr = "";
|
break;
|
case NUMERIC:// 数字
|
if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {// 时间
|
DateFormat format = new SimpleDateFormat(Pattern);
|
Date date = cell.getDateCellValue();
|
cellStr = format.format(date);
|
break;
|
} else {
|
cellStr = String.valueOf(cell.getNumericCellValue());
|
if (cellStr.split("\\.").length == 2) {// 去.0
|
String[] ss = cellStr.split("\\.");
|
if (ss[1].equals("0")) {
|
cellStr = ss[0] + "";
|
}
|
}
|
}
|
break;
|
default:
|
cellStr = cell.getStringCellValue();
|
break;
|
}
|
}
|
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);
|
}
|
|
}
|