不能让我的Excel阅读的Apache POI工作

我正在尝试使用这个基本的布局来读取一个Excel文件的程序。 我想扩展超越这个代码,但由于某种原因,每当我构build代码,我得到一个错误“注意:C:\\用户\ Ryan Kabir \ Documents \ NetBeansProjects \ ExcelReader \ src \ excelreader \ ExcelReader.java使用或覆盖注意:使用-Xlint:deprecation重新编译以获取详细信息。 我试着用javac -Xlint:deprecation ExcelReader.java进行编译,但是我找不到哪个方法不推荐使用。 对不是我的testingExcel文件只是一个3×6表。

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; /** * A dirty simple program that reads an Excel file. * @author www.codejava.net * */ public class ExcelReader { public static void main(String[] args) throws IOException { String excelFilePath = "Books.xlsx"; FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); Workbook workbook = new XSSFWorkbook(inputStream); Sheet firstSheet = workbook.getSheetAt(0); Iterator<Row> iterator = firstSheet.iterator(); while (iterator.hasNext()) { Row nextRow = iterator.next(); Iterator<Cell> cellIterator = nextRow.cellIterator(); while (cellIterator.hasNext()) { Cell cell = cellIterator.next(); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.print(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: System.out.print(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_NUMERIC: System.out.print(cell.getNumericCellValue()); break; } System.out.print(" - "); } System.out.println(); } workbook.close(); inputStream.close(); } } 

Cell.getCellType以及Cell中的所有字段都被弃用。 使用Cell.getCellTypeEnum和CellType来代替获取单元格内容 。

它需要有点改变,因为

 error: an enum switch case label must be the unqualified name of an enumeration constant case CellType.STRING: 

但是,以下应该工作:

 import org.apache.poi.ss.usermodel.CellType; ... switch (cell.getCellTypeEnum()) { case STRING: System.out.println(cell.getRichStringCellValue().getString()); break; case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.println(cell.getDateCellValue()); } else { System.out.println(cell.getNumericCellValue()); } break; case BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case FORMULA: System.out.println(cell.getCellFormula()); break; case BLANK: System.out.println(); break; default: System.out.println(); } ... 

繁忙的开发者指南HSSF和XSSF特性总是很好的。