我正在尝试将这种基本布局用于可以读取Excel文件的程序。我想扩展此代码,但由于每次构建代码的原因,都会出现错误“注意:C:\ Users \ Ryan Kabir \ Documents \ NetBeansProjects \ ExcelReader \ src \ excelreader \ ExcelReader.java使用或覆盖不推荐使用的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();
    }

}

最佳答案

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。因此,使用那些版本,我们现在需要使用此方法。

10-01 19:15