无法为我的 Excel 阅读工作获取 Apache POI
Cannot get Apache POI for my Excel reading to work
我正在尝试将此基本布局用于可以读取 Excel 文件的程序。我想扩展的不仅仅是这段代码,但出于某种原因,每次构建代码时,我都会收到错误消息“注意:C:\Users\Ryan Kabir\Documents\NetBeansProjects\ExcelReader\src\excelreader\ExcelReader.java 使用或覆盖了已弃用的 API。
注意:使用 -Xlint:deprecation 重新编译以获得详细信息。”我尝试使用 javac -Xlint:deprecation ExcelReader.java 进行编译,但我找不到不推荐使用的方法。
不是我的测试 excel 文件只是一个 3x6 table.
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 as well as all the fields in Cell are deprecated. Use Cell.getCellTypeEnum and CellType instead as shown in 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 returns CellType。所以使用这些版本我们现在需要使用这种方法。
我正在尝试将此基本布局用于可以读取 Excel 文件的程序。我想扩展的不仅仅是这段代码,但出于某种原因,每次构建代码时,我都会收到错误消息“注意:C:\Users\Ryan Kabir\Documents\NetBeansProjects\ExcelReader\src\excelreader\ExcelReader.java 使用或覆盖了已弃用的 API。 注意:使用 -Xlint:deprecation 重新编译以获得详细信息。”我尝试使用 javac -Xlint:deprecation ExcelReader.java 进行编译,但我找不到不推荐使用的方法。 不是我的测试 excel 文件只是一个 3x6 table.
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 as well as all the fields in Cell are deprecated. Use Cell.getCellTypeEnum and CellType instead as shown in 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 returns CellType。所以使用这些版本我们现在需要使用这种方法。