I get an IOException with the code below. Whats the problem? What I want is to Read an Excel File xlsm and xls or xlsx via the filedialog after the import read the excel and work with it.
public class start {
public static void main(String[] args) throws IOException, InvalidFormatException {
JFrame yourJFrame = new JFrame();
FileDialog fd = new FileDialog(yourJFrame, "Choose a file", FileDialog.LOAD);
fd.setVisible(true);
String filename = fd.getFile();
String filepth = fd.getDirectory();
Workbook workbook = WorkbookFactory.create(new File(filepth + filename));
System.out.println("Workbook has " + workbook.getNumberOfSheets() + " Sheets : ");
Iterator<Sheet> sheetIterator = workbook.sheetIterator();
System.out.println("Retrieving Sheets using Iterator");
while (sheetIterator.hasNext()) {
Sheet sheet = sheetIterator.next();
System.out.println("=> " + sheet.getSheetName());
}
String
DataFormatter dataFormatter = new DataFormatter();
System.out.println("\n\nIterating over Rows and Columns using Iterator\n");
Iterator<Row> rowIterator = sheet.rowIterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
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();
}
workbook.close();
}
}