Java通过FileDialog读取Excel文件

时间:2018-10-09 14:14:07

标签: java excel apache-poi xlsx xlsm

我得到一个带有以下代码的IOException。有什么问题? 我想要的是在导入后读取Excel并使用它来通过filedialog读取Excel文件xlsm和xls或xlsx。

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();
}
}

0 个答案:

没有答案