根據文件路徑獲取excel文件,對文件進行解析
public static Map analysisExcel(String filepath) throws IOException { log.info(filepath); log.info(System.getProperty("user.dir")+ File.separator); log.info("服務器一====="+System.getProperty("user.dir")+ File.separator+"upload"+File.separator+filepath); log.info("服務器二====="+File.separator+"home"+File.separator+"bonc"+File.separator+"netaxtest"+File.separator+"upload"+File.separator+filepath); StringBuilder stringBuilder = new StringBuilder(); int fileNumList = 0; Map<String,Object> map = new HashMap<>(); //String encoding = "GBK"; //獲取服務器上的文件路徑 //File excel = new File(System.getProperty("user.dir")+ File.separator+"upload"+File.separator+filepath);//根據文件路徑獲取對應的excel //獲取本地文件路徑 File excel = new File(System.getProperty("user.dir")+ File.separator+"upload"+File.separator+filepath); if (excel.isFile() && excel.exists()) { //判斷文件是否存在 log.info("文件存在,已判斷文件存在,進入excel解析階段"); String[] split = excel.getName().split("\\."); //.是特殊字符,需要轉義!!!!! Workbook wb=null; //根據文件后綴(xls/xlsx)進行判斷 if ( "xls".equals(split[1])){ FileInputStream fis = new FileInputStream(excel); //文件流對象 wb = new HSSFWorkbook(fis); }else if ("xlsx".equals(split[1])){ FileInputStream fis = new FileInputStream(excel); wb = new XSSFWorkbook(fis); }else { return null; } int sheetCount = wb.getNumberOfSheets(); log.info("sheetCount文件有幾個sheet========"+sheetCount); for(int i = 0;i<sheetCount;i++){ //開始解析 Sheet sheet = wb.getSheetAt(i);//讀取sheet 0 Row r = sheet.getRow(0); //讀取表頭信息,進行循環保存 for (int j=0; j<r.getLastCellNum(); j++){ String cellData = (String) getCellFormatValue(r.getCell(j)); if(!ObjectUtils.isEmpty(cellData)){//過濾掉為null或“”的數據 if(stringBuilder.length()==0){ stringBuilder.append(cellData.replaceAll(" ", "")); }else{ stringBuilder.append(",").append(cellData.replaceAll(" ", "")); } } } fileNumList = fileNumList + sheet.getLastRowNum();//獲取excel中數據的條數並進行相加 log.info("文件數據量fileNumList========"+fileNumList); log.info("文件表頭stringBuilder========"+stringBuilder); //String sheetName = sheet.getSheetName();//sheet頁名稱 //int firstRowIndex = sheet.getFirstRowNum()+1; //第一行是列名,所以不讀 //fileNumList.add(String.valueOf(sheet.getLastRowNum()));//獲取行數 } } else { //找不到指定文件 log.info("未找到指定文件"); log.info(System.getProperty("user.dir")+ File.separator+"upload"+File.separator+filepath); } map.put("stringBuilder",stringBuilder);//該條數據的數據項 map.put("fileNumList",fileNumList);//數據條數 return map; }
private static Object getCellFormatValue(Cell cell){ Object cellValue = null; if(cell!=null){ //判斷cell類型 CellType cellType = CellType.forInt(cell.getCellType()); switch(cellType){ case NUMERIC:{ if(DateUtil.isCellDateFormatted(cell)){ //excel文件內的日期列若設置單元格格式為日期,讀取的值和文件內看到的不一樣, //需要根據num對應的格式進行轉換,最好是直接在上傳頁面強制要求單元格格式為文本一勞永逸, //因為num對應的日期格式不同版本的jar包不一樣,成本太高但是工期夠就無所謂了可慢慢研究。 short num = cell.getCellStyle().getDataFormat(); //String format = ExcelConstant.dateFormatMap.get(num); SimpleDateFormat df = new SimpleDateFormat(String.valueOf(num)); cellValue = df.format(cell.getDateCellValue()); }else{ cell.setCellType(CellType.STRING); //將數值型cell設置為string型 cellValue = cell.getStringCellValue(); } break; } case FORMULA:{ //判斷cell是否為日期格式 if(DateUtil.isCellDateFormatted(cell)){ //轉換為日期格式YYYY-mm-dd cellValue = cell.getDateCellValue(); }else{ //數字 cellValue = String.valueOf(cell.getNumericCellValue()); } break; } case STRING:{ cellValue = cell.getRichStringCellValue().getString(); break; } default: cellValue = cell.getRichStringCellValue().getString(); break; } }else{ cellValue = ""; } return cellValue; }