Java 根据文件路径解析excel

根据文件路径获取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;
    }

 

posted @ 2022-02-18 15:23  krt-wanyi  阅读(1369)  评论(0编辑  收藏  举报