从 Java 中预先存在的 Excel 文件中读取数据
Read data from pre-existing Excel file in Java
我在从预先存在的 Excel 文件中读取数据时遇到问题。
文件名的示例是 "Donors By Last Name - Thu Dec 15 08/20/40 PST 2016.xls"
我的方法是这样的:
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();
}
}
我知道该文件确实存在,但是当我 运行 程序时我得到这个错误:
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)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at Directory.addDonorsFF(Directory.java:115)
at Driver.main(Driver.java:24)
我希望有一些简单的东西,这只是我的头脑,因为我是初学者。你有什么建议吗?
你说你的文件在你的桌面上,但你没有在你的文件名中完全限定要打开的文件。这应该类似于 "C:\users\myname\desktop\Donors..."(如果 Windows)。
您输入文件名的方式,它只会在您所在的当前文件夹中查找运行您申请的文件。
我在从预先存在的 Excel 文件中读取数据时遇到问题。
文件名的示例是 "Donors By Last Name - Thu Dec 15 08/20/40 PST 2016.xls"
我的方法是这样的:
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();
}
}
我知道该文件确实存在,但是当我 运行 程序时我得到这个错误:
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)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.(FileInputStream.java:138)
at Directory.addDonorsFF(Directory.java:115)
at Driver.main(Driver.java:24)
我希望有一些简单的东西,这只是我的头脑,因为我是初学者。你有什么建议吗?
你说你的文件在你的桌面上,但你没有在你的文件名中完全限定要打开的文件。这应该类似于 "C:\users\myname\desktop\Donors..."(如果 Windows)。
您输入文件名的方式,它只会在您所在的当前文件夹中查找运行您申请的文件。