java-poi 批量匯入excel資料
阿新 • • 發佈:2022-02-18
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集合後,就可以對資料進行處理或者儲存了