/** * Excel导入信息提取 * 注意文件尽量xls,xlsx后缀在不同环境可能导致读取时空指针 * @param param * @return map 后台添加成功或失败的信息 * @throws Exception Map<String,String> * @author gongsy 2023-05-29 */ public List<List<List<Map<String, String>>>> getExelInfoSheet(int sheetNum, Map<String, String> param) throws Exception { //文件存储的本地地址 String path = ExcelImportConstans.EXCEL_UPLOAD_PATH + "\\" + param.get("id") + ".xls"; File file=new File(path); //读取结果 List<List<List<Map<String, String>>>> listArrayList = new ArrayList<>(); try { // 创建工作簿 FileInputStream is = new FileInputStream(file); Workbook workbook = WorkbookFactory.create(is); logger.info("文件地址:"+workbook.getSheetName(0)); // 遍历sheet表单 for (int i = 0; i < sheetNum; i++) { List<List<Map<String, String>>> lists = new ArrayList<>(); //读取sheet表单信息 Sheet sheet1 = workbook.getSheetAt(i); //遍历表单中的数据,按行读取 for (Row row : sheet1) { //记录表单数据所在行,列信息以及具体值 List<Map<String, String>> mapList = new ArrayList<>(); for (Cell cell : row) { Map<String, String> infoMap = new HashMap<>(); //列信息 infoMap.put("loacl", cell.getColumnIndex() + ""); //具体内容 infoMap.put("value", cell.toString()); //行信息 infoMap.put("row", cell.getRowIndex() + ""); mapList.add(infoMap); } lists.add(mapList); } //记录当前工作薄信息 listArrayList.add(lists); // 关闭工作簿和文件流 workbook.cloneSheet(i); } //关闭io流 is.close(); } catch (IOException e) { e.printStackTrace(); } //返回整理后的表信息 return listArrayList; }
JAVA POI读取EXECL文件片段
于 2023-07-03 15:58:45 首次发布