简体   繁体   English

从 Java 中预先存在的 Excel 文件中读取数据

[英]Read data from pre-existing Excel file in Java

I'm having trouble reading data from a pre-existing Excel file.我在从预先存在的 Excel 文件中读取数据时遇到问题。

An example of a fileName is "Donors By Last Name - Thu Dec 15 08/20/40 PST 2016.xls"文件名的一个例子是“Donors By Last Name - Thu Dec 15 08/20/40 PST 2016.xls”

This is what my method looks like:这是我的方法的样子:

public void addDonorsFF() throws IOException
{
    JTextField a = new JTextField(20);
    Object[] message = {"Enter File Name:", a, "\nIt is best to directly copy paste the file name, including .xls \nYou cannot import Shipping files."};
    int option = JOptionPane.showConfirmDialog(null, message, "Select File", JOptionPane.OK_CANCEL_OPTION);
    if (option == JOptionPane.OK_OPTION)
    {
        String fileName = (String)a.getText();
        FileInputStream file = new FileInputStream(new File(fileName));

        //Create Workbook instance holding reference to .xls file
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        //Get sheet from the workbook
        HSSFSheet sheet = workbook.getSheetAt(0);

        for(int i = 1; i < sheet.getPhysicalNumberOfRows(); i++)
        {
            Row row = sheet.getRow(i);

            for(int j = 0; j < row.getPhysicalNumberOfCells(); j++)
            {
                Cell cell = row.getCell(j);

                //Some code that uses the data in the cell and puts it in a "donor" object;
            }
        }
        workbook.close();


    }
}

I know the file exists for certain, but when I run the program I get this error:我知道该文件肯定存在,但是当我运行该程序时,出现此错误:

Exception in thread "main" java.io.FileNotFoundException: Donors By Last Name - Thu Dec 15 08/20/40 PST 2016.xls (No such file or directory)线程“main”中的异常 java.io.FileNotFoundException: Donors By Last Name - Thu Dec 15 08/20/40 PST 2016.xls(没有这样的文件或目录)

at java.io.FileInputStream.open0(Native Method)在 java.io.FileInputStream.open0(本机方法)

at java.io.FileInputStream.open(FileInputStream.java:195)在 java.io.FileInputStream.open(FileInputStream.java:195)

at java.io.FileInputStream.(FileInputStream.java:138)在 java.io.FileInputStream.(FileInputStream.java:138)

at Directory.addDonorsFF(Directory.java:115)在 Directory.addDonorsFF(Directory.java:115)

at Driver.main(Driver.java:24)在 Driver.main(Driver.java:24)

I hope there's something simple, this is just going over my head because I'm a beginner.我希望有一些简单的事情,因为我是初学者,这只是我的头脑。 Do you have any suggestions?你有什么建议吗?

You say your file is on your desktop, but you didn't fully qualify that in your file name to open.你说你的文件在你的桌面上,但你没有在你的文件名中完全限定它可以打开。 This should be something like "C:\\users\\myname\\desktop\\Donors..." (if Windows).这应该类似于“C:\\users\\myname\\desktop\\Donors...”(如果是 Windows)。

The way you entered the file name, it will only look in the current folder where you are running your application for the file.根据您输入文件名的方式,它只会在您运行文件应用程序的当前文件夹中查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Eclipse中将多种数据类型写入到预先存在的文件中 - Writing multiple data types into a pre-existing file in eclipse Java分配:从预先存在/已分配的池中分配对象 - Java allocation : allocating objects from a pre-existing/allocated pool 将属性文件添加到预先存在的PropertyPlaceholderConfigurer - Add a properties file to the pre-existing PropertyPlaceholderConfigurer 替换预先存在的 jar 文件中的字符串 - Replace string in pre-existing jar file 是否可以在我的程序中编辑预先存在的.class文件? - Is it possible to edit a pre-existing .class file from within my program? 如何将字符串中的文本添加到预先存在的 txt 文件中? - How do I add text from a string to a pre-existing txt file? 将数据从Android应用程序连接并插入到预先存在的MS SQL数据库中 - Connecting and inserting data from an Android App to a pre-existing MS SQL database 有没有办法在Java弹性查询中使用预先存在的json查询? - Is there a way to use a pre-existing json query in a java elastic query? 是否有一个预先存在的类来定义Java中对数字的一系列运算? - Is there a pre-existing class to define a sequence of operations on numbers in Java? 如何将Cucumber测试添加到预先存在的Gradle项目(Java)? - How add Cucumber tests to a pre-existing Gradle project (Java)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM