需要的依賴(lài)
<!-- 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";
/**
* 描述:根據(jù)文件后綴,自適應(yīng)上傳文件的版本
*
* @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文件
* 注意:在使用的過(guò)程中吸耿,文件中每一列的的信息必需和對(duì)象屬性一 一對(duì)應(yīng)
* 讀取excel 中的文本信息
*
* @param wb wb
* @param sheet 需要讀取的sheet
* @param row 數(shù)據(jù)從哪一行開(kāi)始讀取
* @param tClass 對(duì)象
* @param format 指定格式中的時(shí)間格式 默認(rèn)時(shí)間格式y(tǒng)yyy-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);
//獲得工作表的總行數(shù)
int sheetRow = sheetAt.getPhysicalNumberOfRows();
while (row < sheetRow) {
//獲得總列數(shù)
int physicalNumberOfCells = sheetAt.getRow(0).getPhysicalNumberOfCells();
//獲得屬性數(shù)組
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中值,都是設(shè)置成string類(lèi)型進(jìn)行獲取
cell.setCellType(CellType.STRING);
String stringCellValue = cell.getStringCellValue();
//讀取excel中的值,如果為空酷窥,直接進(jìn)入下一列
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("\\.");
//得到數(shù)據(jù)類(lèi)型
String type = split[split.length - 1];
Method method = tClass.getDeclaredMethod(tMethodName, fields[i].getType());
//如果還有其它類(lèi)型咽安,只需要在這里進(jìn)行相應(yīng)的補(bǔ)充就可以
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數(shù)據(jù)異常, exception:{}", e);
} finally {
}
return result;
}
/**
* 轉(zhuǎn)換時(shí)間格式
*
* @param dataStr 時(shí)間字符串
* @param format 需要轉(zhuǎn)換的格式信息
* @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);
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者