gpt4 book ai didi

java - 无法获取 Apache POI 来进行 Excel 阅读

转载 作者:行者123 更新时间:2023-11-30 02:23:10 25 4
gpt4 key购买 nike

我正在尝试将此基本布局用于可以读取 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 。因此,使用这些版本我们现在需要使用此方法。

关于java - 无法获取 Apache POI 来进行 Excel 阅读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46308338/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com