【发布时间】:2018-07-16 12:01:18
【问题描述】:
所以我创建了一个类来搜索一个 excel 文件并打印该文件的所有行和列。现在我有一个找不到异常文件的错误。为了解决此错误,我更改了代码中的文件路径,但由于某种原因,我得到了相同的错误,并且控制台中的错误消息显示我使用的上一个文件路径给了我错误。
我知道新文件路径是正确的,但 Eclipse 似乎没有识别出代码已更新。这是该课程的代码:
public class ExcelReader {
public static final String SAMPLE_XLSX_FILE_PATH = "K:\\Documents\\Project\\Netword_GUI\\Netword_GUI\\src\\libs\\cc2017.xlsx";
public static void main(String[] args) throws IOException, InvalidFormatException {
// Creating a Workbook from an Excel file (.xls or .xlsx)
Workbook workbook = WorkbookFactory.create(new File(SAMPLE_XLSX_FILE_PATH));
// Retrieving the number of sheets in the Workbook
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
/*
=============================================================
Iterating over all the sheets in the workbook (Multiple ways)
=============================================================
*/
// You can obtain a sheetIterator and iterate over it
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
//System.out.println(sheet.getRow(0));
System.out.println("=> " + sheet.getSheetName());
}
// Getting the Sheet at index zero
Sheet sheet = workbook.getSheetAt(0);
// Create a DataFormatter to format and get each cell's value as String
DataFormatter dataFormatter = new DataFormatter();
// You can obtain a rowIterator and columnIterator and iterate over them
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
// Now let's iterate over the columns of the current row
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
String cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\t");
}
System.out.println();
}
if (sheet.getActiveCell() == null) {
// Closing the workbook
workbook.close();
}
}
}
现在这是控制台中显示的错误消息:
线程“主”java.io.FileNotFoundException 中的异常: C:\Users\User\Dropbox\Placement\Private_Backup\Netword_GUI\Netword_GUI\src\cc2017.xlsx 在 org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250) 在 org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:226) 在 org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:205) 在 ExcelReader.main(ExcelReader.java:13)
注意:控制台中的文件路径与代码中的文件路径不同。以前错误消息中的文件路径在代码中,这是因为我在另一台计算机上具有相同的类,因此文件路径已更改,因为我当前正在使用的计算机上的位置不同。
【问题讨论】:
-
尝试清理并重新构建您的项目(从“项目”菜单)。
-
谢谢我已经尝试过了,但现在我收到了这个错误。错误:无法找到或加载主类 ExcelReader
-
@Berger- 请将此作为答案,以便我接受
标签: java filepath eclipse-oxygen