无法获得Apache POI以便我的Excel阅读工作

问题描述 投票:1回答:1

我正在尝试将此基本布局用于可以读取Excel文件的程序。我想扩展到这个代码之外,但由于某种原因,每次构建代码时,都会出现错误“注意:C:\ Users \ Ryan Kabir \ Documents \ NetBeansProjects \ ExcelReader \ src \ excelreader \ ExcelReader.java使用或覆盖不推荐使用的API。注意:使用-Xlint重新编译:弃用以获取详细信息。“我尝试使用javac -Xlint编译:弃用ExcelReader.java但我找不到哪个方法已弃用。对不是我的测试excel文件只是一个3x6表。

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();
    }

}
java excel apache-poi deprecation-warning
1个回答
1
投票

Cell.getCellType以及Cell中的所有字段都已弃用。使用Cell.getCellTypeEnumCellType,如Getting the cell contents所示。

由于它需要稍微改变一下

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()) { //up to apache poi version 3.17
            //switch (cell.getCellType()) { //using apache poi version 4.0.0 upwards

                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();
            }
...

阅读Busy Developers' Guide to HSSF and XSSF Features总是好的。


apache poi版本4.0.0向上,现在Cell.getCellType返回CellType。所以使用这些版本我们现在需要使用这种方法。

© www.soinside.com 2019 - 2024. All rights reserved.