package com.allpayx.atsmpservice.common; import com.allpayx.atsmpservice.param.ResponseParam.RecoredConfirmInfoResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.io.InputStream; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; public class GetExcelInfoUtil { /** 读取Excel信息 * @Company : AllPay * @author : lei.chen * @CreateDate : 2019/2/28 14:11 */ public static List getExcelInfo( MultipartFile mFile) { String fileName = mFile.getOriginalFilename();// 获取文件名 List list = new ArrayList<>(); List error=null; try { if (!validateExcel(fileName)) {// 验证文件名是否合格 return null; } boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本 if (isExcel2007(fileName)) { isExcel2003 = false; } list = createExcel(mFile.getInputStream(), isExcel2003,error); } catch (Exception e) { e.printStackTrace(); } return list; } /** * * @Title: validateExcel * @Description: 验证EXCEL文件 * @return boolean * @author lei * @date 2019/2/28 14:11 */ public static boolean validateExcel(String filePath) { if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) { String errorMsg = "文件名不是excel格式"; return false; } return true; } // 是否是2003的excel,返回true是2003 public static boolean isExcel2003(String filePath) { return filePath.matches("^.+\\.(?i)(xls)$"); } // 是否是2007的excel,返回true是2007 public static boolean isExcel2007(String filePath) { return filePath.matches("^.+\\.(?i)(xlsx)$"); } /** * * @Title: createExcel * @Description: 根据excel里面的内容读取信息 * @return List * @author lei * @date 2019/2/28 14:11 */ public static List createExcel(InputStream is, boolean isExcel2003, List error) { List list = new ArrayList<>(); try { Workbook wb = null; if (isExcel2003) {// 当excel是2003时,创建excel2003 wb = new HSSFWorkbook(is); } else {// 当excel是2007时,创建excel2007 wb = new XSSFWorkbook(is); } list = readExcelValue(wb, error);// 读取Excel里面客户的信息 } catch (IOException e) { e.printStackTrace(); } return list; } /** * * @Title: readExcelValue * @Description: 读取Excel信息 * @return List * @author lei * @date 2019/2/28 14:11 */ public static List readExcelValue(Workbook wb, List error) { int totalRows =0; int totalCells =0; // 得到第一个shell Sheet sheet = wb.getSheetAt(0); // 得到Excel的行数 totalRows = sheet.getPhysicalNumberOfRows(); // 得到Excel的列数(前提是有行数) if (totalRows > 1 && sheet.getRow(0) != null) { totalCells = sheet.getRow(1).getPhysicalNumberOfCells(); } // 循环Excel行数 List list = new ArrayList<>(); for (int r = 1; r < totalRows; r++) { Row row = sheet.getRow(r); if (row == null) { continue; } // T model = new T(); RecoredConfirmInfoResponse model = new RecoredConfirmInfoResponse(); // 循环Excel的列 for (int c = 0; c < totalCells; c++) { Cell cell = row.getCell(c); if (null != cell) { if (c == 0) { System.out.println(cell.getCellType()); if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setCreate_time(cell.getStringCellValue()); } } else if (c == 1) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setDone_time(cell.getStringCellValue()); } } else if (c == 2) { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { model.setBatch_no(new DecimalFormat("0").format(cell.getNumericCellValue())); } } else if (c == 3) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setUser_id(cell.getStringCellValue()); } } else if (c == 4) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setEcp_code(cell.getStringCellValue()); } } else if (c == 5) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setMID(cell.getStringCellValue()); } } else if (c == 6) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setMPID(cell.getStringCellValue()); } } else if (c == 7) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setVoucher_id(cell.getStringCellValue()); } } else if (c == 8) { if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) { model.setAmount(new DecimalFormat("0").format(cell.getNumericCellValue())); } } else if (c == 9) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setCurrency(cell.getStringCellValue()); } } else if (c == 10) { if (cell.getCellType() == Cell.CELL_TYPE_STRING) { model.setProcess_status(cell.getStringCellValue()); } } } } list.add(model); } return list; } }