Java读取Excel

来源:互联网 发布:unity3d everyplay 编辑:程序博客网 时间:2024/06/17 23:05

程序默认跳过表头,从第二行数据开始读:

    public List<String> readMobilesFromExcel(InputStream inputStream) throws Exception {        List<String> result = new ArrayList<String>();        POIFSFileSystem inputPoifsFileSystem = null;        try {            inputPoifsFileSystem = new POIFSFileSystem(inputStream);            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputPoifsFileSystem);            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);            int size = hssfSheet.getLastRowNum();            for (int i = 1; i <= size; i++) {                // 读取一行记录                HSSFRow hssfRow = hssfSheet.getRow(i);                if (hssfRow.getCell(0) == null || "".equals(hssfRow.getCell(0).getStringCellValue().trim())) {                    // 为空的不处理                } else {                    result.add(hssfRow.getCell(0).getStringCellValue().trim());                }            }        } catch (Exception e) {            throw e;        }finally{            inputStream.close();        }        return result;    }
0 0
原创粉丝点击