过程很简单这里不多啰嗦,直接按步骤来!
强调一个注意点:注意实体类导入的poi包,导错了的话对导出的时候有影响!!!
导出的教程在此:jeecg导出多个sheet Excel(干货教学)-CSDN博客
1.引入依赖
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-spring-boot-starter</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-boot-base-core</artifactId>
</dependency>
<dependency>
<groupId>org.jeecgframework.boot</groupId>
<artifactId>jeecg-boot-starter-job</artifactId>
</dependency>
2.创建实体类,这里不多啰嗦 我只创建一个了 大家根据需求创建对应的类就行
package com.test.vo;
import lombok.Data;
import org.jeecgframework.poi.excel.annotation.Excel;
/**
* @author: Mr.Z
* @create: 2024-09-05 13:39
**/
@Data
public class Data1 {
@Excel(name = "唯一标识", orderNum = "0",width = 30.0)
private String id;
//姓名
@Excel(name = "姓名", orderNum = "0",width = 30.0)
private String name;
//年龄
@Excel(name = "年龄", orderNum = "0",width = 30.0)
private String age;
//地址
@Excel(name = "地址", orderNum = "0",width = 60.0)
private String address;
}
3.引入工具类
package com.dev.utils;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import java.io.InputStream;import java.util.List;
import java.util.NoSuchElementException;
/**
* @author Mr.Z
* @version 1.0
* @date 2024/9/5 13:40
*/
public class EasyPoiUtils {
/**
* 功能描述:根据接收的Excel文件来导入多个sheet,根据索引可返回一个集合
*
* @param inputStream excel输入流
* @param pojoClass Excel实体类
* @param sheetIndex 导入sheet索引
* @param titleRows 标题函数
* @param headerRows 表头行数
*
*/
public static <T> List<T> importExcelJeecg(InputStream inputStream, Class<T> pojoClass,int sheetIndex, Integer titleRows, Integer headerRows ) {
org.jeecgframework.poi.excel.entity.ImportParams params = new org.jeecgframework.poi.excel.entity.ImportParams();
//从第几个sheet页开始读取
params.setStartSheetIndex(sheetIndex);
//读取几个sheet页
params.setSheetNum(1);
//标题占几行
params.setTitleRows(titleRows);
//表头占几行
params.setHeadRows(headerRows);
List<T> list = null;
try {
//读取的excel数据集合
list = org.jeecgframework.poi.excel.ExcelImportUtil.importExcel(inputStream, pojoClass, params);
} catch (NoSuchElementException e) {
throw new RuntimeException("模板不能为空");
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
4.调用示例
public Result<?> importExcel(MultipartFile file) throws Exception {
List<Data1> sheet1List = EasyPoiUtils.importExcelJeecg(file.getInputStream(), Data1.class, 0,1,1);
List<Data2> sheet2List = EasyPoiUtils.importExcelJeecg(file.getInputStream(), Data2.class, 1,1,1);
List<Data3> sheet3List = EasyPoiUtils.importExcelJeecg(file.getInputStream(), Data3.class, 2,0,1);
List<Data4> sheet4List = EasyPoiUtils.importExcelJeecg(file.getInputStream(), Data4.class, 3,0,1);
return testService.leadIntoExcel(sheet1List,sheet2List,sheet3List,sheet4List);
}
5.后边的就需要自己根据业务去处理相关数据了
如果此文章帮助到了你,请动动您发财的小手点点赞!