需要的依赖
<!-- poi 解析excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
直接上源码
package org.jeecg.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Created by zl
* @Date 2020/5/14 0:36
* @Description TODO
*/
@Slf4j
public class ExcelUtil {
public static final String excel2003L = ".xls";
public static final String excel2007U = ".xlsx";
/**
* 描述:根据文件后缀,自适应上传文件的版本
*
* @param inStr 将file.getInputStream()获取的输入流
* @param fileName file.getOriginalFilename()获取的原文件名
*/
public static Workbook getWorkbook(InputStream inStr, String fileName) throws Exception {
Workbook wb = null;
String fileType = fileName.substring(fileName.lastIndexOf("."));
if (excel2003L.equals(fileType)) {
wb = new HSSFWorkbook(inStr); // 2003-
} else if (excel2007U.equals(fileType)) {
wb = new XSSFWorkbook(inStr); // 2007+
} else {
throw new Exception("解析的文件格式有误!");
}
return wb;
}
/**
* 读取excel文件
* 注意:在使用的过程中,文件中每一列的的信息必需和对象属性一 一对应
* 读取excel 中的文本信息
*
* @param wb wb
* @param sheet 需要读取的sheet
* @param row 数据从哪一行开始读取
* @param tClass 对象
* @param format 指定格式中的时间格式 默认时间格式yyyy-MM-dd HH:mm:ss
* @param <T>
* @return
*/
public static <T> List<T> getFileRead(Workbook wb, int sheet, int row, Class<T> tClass, String format) {
List<T> result = new ArrayList<>();
try {
//获得工作表
Sheet sheetAt = wb.getSheetAt(sheet);
//获得工作表的总行数
int sheetRow = sheetAt.getPhysicalNumberOfRows();
while (row < sheetRow) {
//获得总列数
int physicalNumberOfCells = sheetAt.getRow(0).getPhysicalNumberOfCells();
//获得属性数组
Field[] fields = tClass.getDeclaredFields();
T t = tClass.newInstance();
for (int i = 0; i < physicalNumberOfCells; i++) {
Cell cell = sheetAt.getRow(row).getCell(i);
if (cell == null) {
continue;
}
//获得sheet中值,都是设置成string类型进行获取
cell.setCellType(CellType.STRING);
String stringCellValue = cell.getStringCellValue();
//读取excel中的值,如果为空,直接进入下一列
if (null == stringCellValue || "".equals(stringCellValue)) {
continue;
}
//获得属性名
String name = fields[i].getName();
//得到set方法
name = name.substring(0, 1).toUpperCase() + name.substring(1);
String tMethodName = "set" + name;
String[] split = fields[i].getType().getName().split("\\.");
//得到数据类型
String type = split[split.length - 1];
Method method = tClass.getDeclaredMethod(tMethodName, fields[i].getType());
//如果还有其它类型,只需要在这里进行相应的补充就可以
switch (type) {
case "Date":
method.invoke(t, getDate(stringCellValue, format));
break;
case "Integer":
method.invoke(t, Integer.valueOf(stringCellValue));
break;
case "Byte":
method.invoke(t, Byte.valueOf(stringCellValue));
break;
case "Double":
method.invoke(t, Double.valueOf(stringCellValue));
break;
case "BigDecimal":
method.invoke(t, BigDecimal.valueOf(stringCellValue.contains("\\.") ? Double.valueOf(stringCellValue) : Integer.valueOf(stringCellValue)));
break;
default:
method.invoke(t, stringCellValue);
break;
}
}
result.add(t);
//读取下一行
row++;
}
} catch (Exception e) {
e.printStackTrace();
log.error("解析excel数据异常, exception:{}", e);
} finally {
}
return result;
}
/**
* 转换时间格式
*
* @param dataStr 时间字符串
* @param format 需要转换的格式信息
* @return
* @throws ParseException
*/
private static Date getDate(String dataStr, String format) throws ParseException {
if (null == format || "".equals(format)) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
return simpleDateFormat.parse(dataStr);
}
}