话不多说直接上代码:
maven
<!--操作excol表格-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
javaBean
@ToString
@Data
class User{
String name;
String sex;
Integer age;
String bir;
}
启动类
public static void main(String[] args) {
//文件所在地
String excelFileName = "C:\\Users\\StaryL\\Desktop\\read.xlsx";
//读取文件得到分装好的数据
List<User> readResult = ExcelReader.readExcel(excelFileName);
System.out.println(readResult+"readResult++++++++++main");
}
读取文件按照文件全路径
/**
* 读取Excel文件内容
* @param excelFileName 要读取的Excel文件所在路径
* @return 读取结果列表,读取失败时返回null
*/
public static List<User> readExcel(String excelFileName) {
Workbook workbook = null;
FileInputStream inputStream = null;
try {
//文件类别
String fileType = excelFileName.substring(excelFileName.lastIndexOf(".") + 1, excelFileName.length());
System.out.println(fileType+"fileType ++++++++++++++++++++++++++++++++++");
File excelFile = new File(excelFileName);
//判断文件是否存在
if(!excelFile.exists()){
System.out.println("文件不存在!!!!");
return null;
}
//把文件转成流对象
inputStream = new FileInputStream(excelFile);
//按照流对象和文件类别得到
workbook = getWorkbook(inputStream,fileType);
//读取excel中数据
List<User> userList = parseExcel(workbook);
System.out.println(userList+"excel中数据是+++++");
return userList;
} catch (Exception e) {
System.out.println("解析Excel失败,文件名:" + excelFileName + " 错误信息:" + e.getMessage());
e.printStackTrace();
return null;
} finally {
try {
if (null != workbook) {
workbook.close();
}
if (null != inputStream) {
inputStream.close();
}
} catch (Exception e) {
System.out.println("关闭数据流出错!错误信息:" + e.getMessage());
return null;
}
}
}
根据不同文件类型得到对应的操作
/**
* 根据文件后缀名类型获取对应的工作簿对象
* @param inputStream 读取文件的输入流
* @param fileType 文件后缀名类型(xls或xlsx)
* @return 包含文件数据的工作簿对象
* @throws IOException
*/
private static Workbook getWorkbook(FileInputStream inputStream, String fileType) throws IOException {
Workbook workbook = null;
//按照不同文件类型进行不同操作
if("xls".equalsIgnoreCase(fileType)){
workbook = new HSSFWorkbook(inputStream);
} else if(fileType.equalsIgnoreCase("xlsx")){
workbook = new XSSFWorkbook(inputStream);
}
return workbook;
}
按照每页获取每一行的数据
/**
* 解析Excel数据
* @param workbook Excel工作簿对象
* @return 解析结果
*/
private static List<User> parseExcel(Workbook workbook) throws ParseException {
//接受数据
List<User> userList = new ArrayList<>();
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
Sheet sheet = workbook.getSheetAt(sheetNum);
//校验sheet是否合法
if(sheet == null){
//不存在数据就掠过
continue;
}
//读取第几行
int rowStart = 1;
//最后一行
int rowEnd = sheet.getPhysicalNumberOfRows();
//从第一行迭代到最后一行
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
//当前第几行
Row row = sheet.getRow(rowNum);
if (null == row) {
continue;
}
//得到数据对象
User user = convertRowToData(row);
//是否存在数据
if(user == null){
System.out.println("第 " + row.getRowNum() + "行数据不合法,已忽略!");
continue;
}
//放入集合
userList.add(user);
}
}
return userList;
}
按照每一行得到每个格子的数据
/**
* 提取每一行中需要的数据,构造成为一个结果数据对象
*
* 当该行中有单元格的数据为空或不合法时,忽略该行的数据
*
* @param row 行数据
* @return 解析后的行数据对象,行数据错误时返回null
*/
private static User convertRowToData(Row row) throws ParseException {
//数据对象
User user = new User();
//获取名字 String
String name = row.getCell(0).getStringCellValue();
user.setName(name);
//获取性别 String
String sex = row.getCell(1).getStringCellValue();
user.setSex(sex);
//获取生日 Date
Date dir = row.getCell(2).getDateCellValue();
if (null == dir || "".equals(dir)) {
// 生日为空
user.setBir(null);
} else {
SimpleDateFormat sf=new SimpleDateFormat("yyyy/MM/dd");
String format = sf.format(dir);
user.setBir(format);
}
// 获取年龄 Double
Double ageStr = row.getCell(3).getNumericCellValue();
if (null == ageStr || "".equals(ageStr)) {
// 年龄为空
user.setAge(null);
} else {
user.setAge(ageStr.intValue());
}
return user;
}
因为第一行中数据不是我们想要使用的所以在这里舍弃此数据
//读取第几行
int rowStart = 1;
//最后一行
int rowEnd = sheet.getPhysicalNumberOfRows();
输出结果
xlsxfileType ++++++++++++++++++++++++++++++++++
[User(name=张三, sex=男, age=21, bir=2020/01/25), User(name=李四, sex=女, age=12, bir=2015/04/05)]excel中数据是+++++
[User(name=张三, sex=男, age=21, bir=2020/01/25), User(name=李四, sex=女, age=12, bir=2015/04/05)]readResult++++++++++main