package cn.itcast.utils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
@Component
public class POIUtils {
private final static String DATE_FORMAT = "yyyy/MM/dd";
/**
* 获取Excel,将数据转换成 List<T> 的形式
* Excel 数据要求第一行为对象的属性名称
*
* @param filePath 文件路径 "D:\\01temp\\hotels_details.xls"
* @param sheetName sheet名称 表名 "hotel"
* @param tClass 要转换成的实体类
* @param <T>
* @return List对象数组
* @throws IOException
*/
public static <T> List<T> readExcelOfList(String filePath, String sheetName, Class<T> tClass) throws Exception {
List<String> resultMapList = new ArrayList<>();
File file = new File(filePath); //文件路径
FileInputStream inputStream = new FileInputStream(file);
//可以使用WorkbookFactory自动根据Excel类型是XLSX还是XLS自动创建对应的Workbook
// 使用工厂模式 根据文件扩展名 创建对应的Workbook
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheet(sheetName); //设置表名
int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum(); //获取行数 排除第一行
//循环行数
for (int i = 1; i < rowCount + 1; i++) {
//当前行数据
Row row = sheet.getRow(i);
// //创建一个Map集合封装读取到的行数
Map<String, Object> resultMap = new HashMap<>();
//循环当前行的列数据
for (int j = 0; j < row.getLastCellNum(); j++) {
/* //判断表格的数据是否为空 ,如果为空设置默认值 ,也可选择跳过 //若不是重要数据可以选择跳过该行数据
if (Objects.equals(row.getCell(j).getCellType(), CellType.BLANK)) {
*//*continue;//跳过当前行的数据*//*
//根据类型设置默认值;要根据准换的对象变量类型来判断, 例如 price:价格 ;score:分值
if (sheet.getRow(0).getCell(j).toString().equals("price") || sheet.getRow(0).getCell(j).toString().equals("score")) {
row.createCell(j).setCellValue(0);
} else {
row.createCell(j).setCellValue("无");
}
}*/
//根据数据类型 获取值封装到Map集合中去
if (Objects.equals(row.getCell(j).getCellType(), CellType.STRING)) {//字符串类型
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getStringCellValue());
} else if (Objects.equals(row.getCell(j).getCellType(), CellType.NUMERIC)) {//数字类型
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getNumericCellValue());
}else if (Objects.equals(row.getCell(j).getCellType(), CellType.BLANK)) {空值
resultMap.put(sheet.getRow(0).getCell(j).toString(), "");
}else if (Objects.equals(row.getCell(j).getCellType(), CellType.FORMULA)) {//公式
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j).getCellFormula());
}else if (row.getCell(j).getCellStyle().getDataFormatString().equals("m/d/yy")) {//日期
String date = new SimpleDateFormat(DATE_FORMAT).format(row.getCell(j).getDateCellValue());//转换日期格式
resultMap.put(sheet.getRow(0).getCell(j).toString(), date);
}else {//其他类型
resultMap.put(sheet.getRow(0).getCell(j).toString(), row.getCell(j));
}
}
//将Map集合转为Json
JSONObject jsonObject = new JSONObject(resultMap);
//将Json格式转为Json字符添加到集合中
resultMapList.add(jsonObject.toJSONString());
}
// 转化为对象 返回
return JSON.parseArray(resultMapList.toString(), tClass);
}
}
依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
<scope>compile</scope>
</dependency>