java - JFileChooser 和使用 JAVA 从 Excel 文件读取

标签 java excel filechooser

我有以下代码

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.text.html.HTMLDocument.Iterator;

    import org.apache.poi.hssf.usermodel.HSSFCell;
    import org.apache.poi.hssf.usermodel.HSSFRow;
    import org.apache.poi.hssf.usermodel.HSSFSheet;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Workbook;

  public class ExcelRead {
       public static String keep = "";

          public static void main(String[] args) throws IOException {

    // File Openner
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("JComboBox Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Select File");

    button.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent ae) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();

                keep = selectedFile.getName();
                System.out.println(keep);
            }
        }
    });
    frame.add(button);
    frame.pack();
    frame.setVisible(true);
    // end of the File opener
    // read from excel
    File excel = new File(keep);

    FileInputStream fis = new FileInputStream(excel);
    HSSFWorkbook wb = new HSSFWorkbook(fis);


    String sheetName = "Assignments"; //if my tempsheet start with "sheetname" thats okay

    for (int i = 0; i < wb.getNumberOfSheets() - 1; i++) {
        HSSFSheet tmpSheet = wb.getSheetAt(i);
        if (tmpSheet.getSheetName().startsWith(sheetName)) {
            //satırları ve sutunları gez oku

        } else {
                wb.removeSheetAt(i);
            }

        }


    }// end of the main




private static String cellToString(HSSFCell cell) {
    int type;
    Object result;
    type = cell.getCellType();
    switch (type) {
    case 0:
        result = cell.getNumericCellValue();
        break;
    case 1:
        result = cell.getStringCellValue();
        break;
    default:
        throw new RuntimeException("there are no support for this type of cell");
    }

    return result.toString();
}
       }

问题是,当我在选择文件之前运行此代码时出现异常。我想使用 FileChooser 选择一个文件,然后返回该文件名以从 Excel 文件中读取。 输出:

 Exception in thread "main" java.io.FileNotFoundException: 
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(Unknown Source)
    at java.io.FileInputStream.<init>(Unknown Source)
    at ExcelRead.main(ExcelRead.java:54)

最佳答案

FileNotFound 异常是由于在您尝试读取文件时 keep 的值仍然是 ""

这是因为您设置 keep 值的代码位于按钮上的 ActionListener 中。触发此代码的操作(很可能是按下按钮)尚未发生。

试试这个:

public static void chooseFile() {
    JFileChooser fileChooser = new JFileChooser();
    int returnValue = fileChooser.showOpenDialog(null);
    if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();

        keep = selectedFile.getName();
        System.out.println(keep);
    }
}
public static void main(String[] args) throws IOException {

// File Openner
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("JComboBox Test");
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select File");

button.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent ae) {
        chooseFile()
    }
});
frame.add(button);
frame.pack();
frame.setVisible(true);
// end of the File opener
// read from excel

chooseFile() // <- make sure that the file is chosen

File excel = new File(keep);

FileInputStream fis = new FileInputStream(excel);
HSSFWorkbook wb = new HSSFWorkbook(fis);


String sheetName = "Assignments"; //if my tempsheet start with "sheetname" thats okay

for (int i = 0; i < wb.getNumberOfSheets() - 1; i++) {
    HSSFSheet tmpSheet = wb.getSheetAt(i);
    if (tmpSheet.getSheetName().startsWith(sheetName)) {
        //satırları ve sutunları gez oku

    } else {
            wb.removeSheetAt(i);
        }

    }


}// end of the main

关于java - JFileChooser 和使用 JAVA 从 Excel 文件读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039369/

相关文章:

android - 来自android webview的onShowFileChooser()只工作一次

java - Swing 是否支持 Windows 7 风格的文件选择器?

java - Content-Disposition 文件扩展名在 Firefox 上无法正常工作

java - java中的HTTPS Get/Post/Put/delete方法

javascript - 使用 ActiveXObject (JavaScript) 读取 Excel 或 OpenOffice (.ods) 文件

java - 如何在java中备份文件?

java - Selenium 单击 chrome 物理按钮,如菜单、左导航、右导航、书签

java - 访问数组时出错

mysql - 使用 VBA 将 mySQL 数据库中的值提取到 Excel 中

excel - 在列中显示行值