“error: cannot find symbol” using Apache POI

2019-09-03 17:49发布

问题:

This is my code:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Reader {

    public static void read_excel() {

        File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
}

This results in the following error message:

error: cannot find symbol 
File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
symbol: class File
location: class reader

I have set the CLASSPATH for the jar files of the Apache POI library. Here is the content of the CLASSPATH varibale:

.;C:\Users\Username\Desktop\Code\Classes;C:\poi-3.12\poi-3.12-20150511.jar;C:\poi-3.12\poi-ooxml-3.12-20150511.jar;C:\poi-3.12\poi-ooxml-schemas-3.12-20150511.jar;C:\poi-3.12\ooxml-lib\xmlbeans-2.6.0.jar;C:\poi-3.12\lib\commons-codec-1.9.jar;C:\poi-3.12\lib\commons-logging-1.1.3.jar;C:\poi-3.12\lib\junit-4.12.jar; C:\poi-3.12\lib\log4j-1.2.17.jar;C:\poi-3.12\poi-examples-3.12-20150511.jar;C:\poi-3.12\poi-excelant-3.12-20150511.jar;C:\poi-3.12\poi-scratchpad-3.12-20150511.jar

I don't understand why the programme does not compile !

回答1:

Add import statement for File

import java.io.File;


回答2:

try with the following code

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.io.File;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Reader {

    public static void read_excel() throws FileNotFoundException {

        File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
}


回答3:

Ok, this code here finally did not produce an error message:

import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.File;
import java.util.*;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;





public class Reader {

    public static void read_excel()  throws FileNotFoundException, IOException {

        File excel =  new File ("C:\\Users\\Username\\Desktop\\java-Tools\\data.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
}