目录
1、添加依赖
2、测试的excel文件
2.1 测试的excel文件123.xlss
3、实际调用的工具类
4、测试输出的数据
1、添加依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
2、测试的excel文件
2.1 测试的excel文件123.xlss
3、实际调用的工具类
package com.shucha.smartreport.util;
import com.csvreader.CsvReader;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author tqf
* @Description EXCEL文件、CSV文件数据读取返回
* @Version 1.0
* @since 2022-05-07 14:32
*/
public class ReadExcelUtil {
/**
* 读取excel文件数据,所有的sheet数据
* 返回数据格式 [{sheetName=用户信息, data=[{姓名=王海, 年龄=1}, {姓名=李四, 年龄=2}]}, {sheetName=操作记录, data=[{操作记录=方法, 操作人=25}, {操作记录=发发发, 操作人=25}]}, {sheetName=Sheet3, data=[]}]
* @param file
* @return
* @throws IOException
*/
public static List<Map<String, Object>> readExcel(File file) throws IOException {
List<Map<String, Object>> allList = new ArrayList<>();
// 获取excel工作簿对象
String fileName = file.getName();
String substring = fileName.substring(fileName.lastIndexOf("."));
HSSFWorkbook workbook = null;
XSSFWorkbook workbookXss = null;
if (".xls".equals(substring)) {
workbook = new HSSFWorkbook(new FileInputStream(file));
allList = readSheet(workbook, null);
} else if (".xlsx".equals(substring)) {
workbookXss = new XSSFWorkbook(new FileInputStream(file));
allList = readSheet(null, workbookXss);
}
System.out.println(allList);
return allList;
}
/**
* 读取所有sheet数据
* @param workbooks
* @param workbookXss
* @return
*/
public static List<Map<String, Object>> readSheet(HSSFWorkbook workbooks, XSSFWorkbook workbookXss){
List<Map<String, Object>> allList = new ArrayList<>();
for (Sheet sheet:workbooks != null ? workbooks : workbookXss){
List<String> headerList = new ArrayList<>();
List<Map<String, Object>> dataList = new ArrayList<>();
System.out.println(sheet.getSheetName());
int i =0;
for (Row row : sheet) {
if (i == 0) {
if (i == 0) {
for (Cell cell : row) {
headerList.add(cell.getStringCellValue());
}
}
} else {
Map<String, Object> map = new HashMap<>();
int j = 0;
for (Cell cell : row) {
//设置单元格类型
cell.setCellType(CellType.STRING);
map.put(headerList.get(j), cell.getStringCellValue());
j++;
}
dataList.add(map);
}
i++;
}
Map<String, Object> map = new HashMap<>();
map.put("sheetName",sheet.getSheetName());
map.put("data",dataList);
allList.add(map);
}
return allList;
}
public static void main(String[] args) throws IOException {
File file = new File("D:\\123\\123.xlsx");
readExcel(file);
File file1 = new File("D:\\123\\测试excel.xls");
readExcel(file1);
}
}
4、测试输出的数据
用户信息
操作记录
Sheet3
[{sheetName=用户信息, data=[{姓名=王海, 年龄=1}, {姓名=李四, 年龄=2}]}, {sheetName=操作记录, data=[{操作记录=方法, 操作人=25}, {操作记录=发发发, 操作人=25}]}, {sheetName=Sheet3, data=[]}]
Sheet1
操作记录
Sheet3
[{sheetName=Sheet1, data=[{密码=123, 用户名=张三, 年龄=10, 性别=男}, {密码=254, 用户名=李四, 年龄=12, 性别=男}, {密码=214, 用户名=王花, 年龄=36, 性别=女}]}, {sheetName=操作记录, data=[{操作类型=方法, 操作时间=2022, 操作人=王五}, {操作类型=点点滴滴, 操作时间=2021, 操作人=李四}]}, {sheetName=Sheet3, data=[]}]
|