java POI方式解析Excel 代碼

private Workbook getWorkBook(String filePath) {
        //xls-2003, xlsx-2007
        FileInputStream is = null;
        try {
            is = new FileInputStream(filePath);
            if (filePath.toLowerCase().endsWith("xlsx")) {
                return new XSSFWorkbook(is);
            } else if (filePath.toLowerCase().endsWith("xls")) {
                return new HSSFWorkbook(is);
            } else {
                throw new RuntimeException("excel解析異常");
            }
        } catch (IOException e) {
            throw new RuntimeException("excel解析異常", e);
        } finally {
            IOUtils.closeQuietly(is);//需要common-io包
        }
    }
/**下標從0 開始*/
Sheet sheet = workBook.getSheet("sheet1");//一個Excel有多少個sheet
Row row = sheet.getRow(0);//獲取指定sheet的行
Cell cell = row.getCell(0);//獲取行上的單元格
 double value = cell.getNumericCellValue();
 String value = cell.getStringCellValue();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章