java-poi 批量导入excel数据
1,首先,前端发送MultipartFile类型文件,后端接收
2,分别创建多个ImportParams对象(easypoi),对应工作蒲
注意:pom中 要有相对应的配置
<!-- easypoi --> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>3.2.0</version> </dependency>
startSheetIndex 开始读取的sheet位置,默认为0
startRows 字段真正值和列标题之间的距离 默认0
titleRows 表格标题行数,默认0
ImportParams sheet1 = new ImportParams(); sheet1.setStartSheetIndex(0); sheet1.setStartRows(0); sheet1.setTitleRows(0); ImportParams sheet2 = new ImportParams(); sheet2.setStartSheetIndex(1); sheet2.setStartRows(0); sheet2.setTitleRows(0); ImportParams sheet3 = new ImportParams(); sheet3.setStartSheetIndex(2); sheet3.setStartRows(0); sheet3.setTitleRows(1); ImportParams sheet4 = new ImportParams(); sheet4.setStartSheetIndex(3); sheet4.setStartRows(0); sheet4.setTitleRows(1);
3,使用easypoi自带的工具类ExcelImportUtil,将数据读取到集合中
List<MaterialLibPO> materialList = ExcelImportUtil.importExcel(excel.getInputStream(), MaterialLibPO.class, sheet1); List<BigMaterialPO> largeList = ExcelImportUtil.importExcel(excel.getInputStream(), BigMaterialPO.class, sheet2); List<RoutineMaterialPO> conventionalList = ExcelImportUtil.importExcel(excel.getInputStream(), RoutineMaterialPO.class, sheet3); List<ToolLibPO> toolLibList = ExcelImportUtil.importExcel(excel.getInputStream(), ToolLibPO.class, sheet4);
注意:对应的实体类要加 @Excel(name = "对应名称") 这里以MaterialLibPO为实例
@Data @AllArgsConstructor @NoArgsConstructor public class MaterialLibPO implements Serializable { /** * 唯一标识 */ @Excel(name = "抢修物资库UUID") private String id; /** * 地市id */ private String ssdsid; /** * 区县id */ private String ssqxid; /** * 供电单位id */ private String ssgddwid; /** * 抢修站id */ @Excel(name = "抢修站ID") private String qxzid; /** * 物资库名称 */ @Excel(name = "物资库名称(抢修站名+物资库)") private String wzkmc; /** * 移动库个数 */ @Excel(name = "移动库个数(四轮抢修车辆数)") private String ydkgs; /** * 库存状态 */ @Excel(name = "库存状态") private String kczt; /** * 经度 */ private String jd; /** * 纬度 */ private String wd; /** * 所属供电单位名称 */ @Excel(name = "供电单位(市县公司)") private String ssgddwmc; /** * 所属地市名称 */ @Excel(name = "地市") private String ssdsmc; /** * 抢修站名称 */ @Excel(name = "抢修站名称(附表抢修站标准名称)") private String qxzmc; private static final long serialVersionUID = 1L; }
4,获取到list集合后,就可以对数据进行处理或者存储了