POI读取Excel

POI读取Excel

传入文件地址,读取Excel文件

public static void readExcel(String file) throws Exception{
        boolean isExcel2003 = file.toLowerCase().endsWith("xls")?true:false;
        Workbook workbook = null;
        if(isExcel2003){
            workbook = new HSSFWorkbook(new FileInputStream(new File(file)));
        }else{
            workbook = new XSSFWorkbook(new FileInputStream(new File(file)));
        }
        Sheet sheet = workbook.getSheetAt(0);
        Row row = null;
        for (int i = 1; sheet.getRow(i)!=null; i++) {
            row = sheet.getRow(i);
            //日起类型的格式化
            String pagetime = "";
            if(row.getCell(2).getCellType() == Cell.CELL_TYPE_STRING){  
                row.getCell(2).setCellType(Cell.CELL_TYPE_NUMERIC);  
            }
            if(row.getCell(2).getCellType()==Cell.CELL_TYPE_NUMERIC){ 
                if (HSSFDateUtil.isCellDateFormatted(row.getCell(2))) { //Excel Date类型处理  
                    Date date = HSSFDateUtil.getJavaDate(row.getCell(2).getNumericCellValue());     
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
                    pagetime = sdf.format(date);
                }
            }
            String address = getCellValue(row.getCell(1));
            String name = getCellValue(row.getCell(3));
            String tel = getCellValue(row.getCell(4));
        }
    }

过滤获取的数据格式

public static String getCellValue(Cell cell){  
        String cellValue = "";  
        if(cell == null){  
            return cellValue;  
        } 
        int cellType = cell.getCellType();
        if(cellType == Cell.CELL_TYPE_NUMERIC){  
            cell.setCellType(Cell.CELL_TYPE_STRING);  
        }  
        if(cellType==Cell.CELL_TYPE_NUMERIC){ //数字
            cellValue = String.valueOf(cell.getNumericCellValue()); 
        }else if(cellType==Cell.CELL_TYPE_STRING){ //字符串
            cellValue = String.valueOf(cell.getStringCellValue());  
        }else if(cellType==Cell.CELL_TYPE_BOOLEAN){ //Boolean
            cellValue = String.valueOf(cell.getBooleanCellValue());
        }else if(cellType==Cell.CELL_TYPE_FORMULA){ //公式
            cellValue = String.valueOf(cell.getCellFormula());
        }else if(cellType==Cell.CELL_TYPE_BLANK){ //空值
            cellValue = "";
        }else(cellType==Cell.CELL_TYPE_ERROR){ //故障  
            cellValue = "未知类型";
        }
        return cellValue;  
    }

如果是导数据到服务器的话,我是先上传,最后处理完了再删除。

public static boolean delete(String path)
    {
        boolean flag=false;
        File f = new File(path);
        if(f.exists()){
            flag = f.delete();
        }
        return flag;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值