java中如何使用Apache Poi从Excel工作表中读取单元格值呢?
下文笔者讲述使用Apache Poi从Excel工作表中读取单元格值的方法分享,如下所示:
实现思路: 1.将excel读取为流 2.使用Poi类读取单元格例:
FileInputStream fis = new FileInputStream("/test/test.xls"); Workbook wb = new HSSFWorkbook(fis); //or new XSSFWorkbook("/test/test.xls") Sheet sheet = wb.getSheetAt(0); FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator(); // suppose your formula is in B3 CellReference cellReference = new CellReference("B3"); Row row = sheet.getRow(cellReference.getRow()); Cell cell = row.getCell(cellReference.getCol()); if (cell!=null) { switch (evaluator.evaluateFormulaCell(cell)) { case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: System.out.println(cell.getNumericCellValue()); break; case Cell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BLANK: break; case Cell.CELL_TYPE_ERROR: System.out.println(cell.getErrorCellValue()); break; // CELL_TYPE_FORMULA will never occur case Cell.CELL_TYPE_FORMULA: break; } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。