package com.mokamrp.privates.help; import com.mokamrp.privates.entity.VoltaHandle; import com.mokamrp.privates.utils.StringUtils; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; /** * @Author:huang * @Date:2019-09-10 10:29 * @Description:excel文件解析类 */ public class Analysis { public Analysis() { throw new Error("工具类不允许实例化!"); } /** * 获取并解析excel文件,返回一个二维集合 * @param file 上传的文件 * @return 二维集合(第一重集合为行,第二重集合为列,每一行包含该行的列集合,列集合包含该行的全部单元格的值) */ public static VoltaHandle>> analysis(MultipartFile file, Map field) { VoltaHandle>> res = new VoltaHandle<>(); ArrayList> row = new ArrayList<>(); if(field.size()<=0){ res.setErr("表格每列绑定参数为空,请先绑定"); return res; } //获取文件名称 String fileName = file.getOriginalFilename(); System.out.println(fileName); try { //获取输入流 InputStream in = file.getInputStream(); //判断excel版本 Workbook workbook = null; if (judegExcelEdition(fileName)) { workbook = new XSSFWorkbook(in); } else { workbook = new HSSFWorkbook(in); } //获取第一张工作表 Sheet sheet = workbook.getSheetAt(0); //从第二行开始获取 for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) { //循环获取工作表的每一行 Row sheetRow = sheet.getRow(i); if (sheetRow == null){ continue; } //.如果每行首列为空 直接视为本行没数据 if(Analysis.getCellValue(sheetRow.getCell(0)).isEmpty()){ continue; } //循环获取每一列 Map cell = new HashMap<>(); for (int j = 0; j < sheet.getRow(0).getPhysicalNumberOfCells(); j++) { if (field.get(j) == null){ continue; } String[] param = field.get(j).split("\\|"); String paramKey = param[0]; String paramValue = null; String paramRule = null; if (param.length > 1){ paramRule = param[1]; } String cellStr = Analysis.getCellValue(sheetRow.getCell(j)); //如果列数据为空,检查是否允许此列数据为空 if (cellStr.isEmpty()) { if (paramRule == null){ String collName = Analysis.getCellValue(sheet.getRow(0).getCell(j)); res.setErr("第"+i+"行,("+collName+")列数据不可为空,请检查表格"); return res; }else if (paramRule.equals("default")) { continue; }else { paramValue = paramRule; } }else{ paramValue = cellStr; } if(j>=field.size()){ break; } cell.put(paramKey,paramValue); } if (!cell.isEmpty()){ //将装有每一列的集合装入大集合 row.add(cell); } //关闭资源 workbook.close(); } } catch (FileNotFoundException e) { res.setErr("未找到文件"); return res; } catch (IOException e) { res.setErr("上传时报"); return res; } res.setData(row); return res; } /** * 判断上传的excel文件版本(xls为2003,xlsx为2017) * @param fileName 文件路径 * @return excel2007及以上版本返回true,excel2007以下版本返回false */ private static boolean judegExcelEdition(String fileName){ if (fileName.matches("^.+\\.(?i)(xls)$")){ return false; }else { return true; } } public static String getCellValue(Cell cell) { String value = ""; if (cell != null) { // 以下是判断数据的类型 switch (cell.getCellTypeEnum()) { case NUMERIC: // 数字 cell.setCellType(CellType.STRING); value = cell.getStringCellValue() + ""; break; case STRING: // 字符串 value = cell.getStringCellValue(); break; case BOOLEAN: // Boolean value = cell.getBooleanCellValue() + ""; break; case FORMULA: // 公式 value = cell.getCellFormula() + ""; break; case BLANK: // 空值 value = ""; break; case ERROR: // 故障 value = "非法字符"; break; default: value = ""; break; } } return value.trim(); } }