【问题标题】:Cannot get Apache POI for my Excel reading to work无法让我的 Excel 阅读工作的 Apache POI
【发布时间】:2017-09-19 19:25:48
【问题描述】:

我正在尝试将此基本布局用于可以读取 Excel 文件的程序。我想扩展此代码之外的内容,但由于某种原因,每次构建代码时,都会出现错误“注意:C:\Users\Ryan Kabir\Documents\NetBeansProjects\ExcelReader\src\excelreader\ExcelReader.java uses or overrides已弃用的 API。 注意:使用 -Xlint:deprecation 重新编译以获取详细信息。”我尝试使用 javac -Xlint:deprecation 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();
    }

}

【问题讨论】:

  • 您使用的是哪个版本的apache-poi?发布您的依赖项
  • 这不是错误,而是警告。这不是您未指定的不起作用的原因。

标签: java excel apache-poi deprecation-warning


【解决方案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。所以使用我们需要的那些版本,现在就使用这个方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-19
    • 2016-02-20
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多