Java .xlsx Excel files in Java: Sheet & Workbook error

Click For Summary
The discussion centers on accessing Excel .xlsx files in Java using the Apache POI library. The user is encountering compilation errors related to missing symbols for the `Sheet` and `Workbook` classes, as well as issues with the `WorkbookFactory.create` method. Suggestions include checking the import statements, as the user may not be importing the correct packages. It's recommended to only import the package names rather than specific classes, and to ensure that the necessary libraries are correctly included in the project. Additionally, using an IDE like NetBeans, Eclipse, or IntelliJ can help manage imports and identify errors more effectively. A link to the Apache POI documentation is provided for reference on the correct package structure.
zak100
Messages
462
Reaction score
11
Hi,
I am trying to access Excel Sheet .xlsx files in Java. I have tried various options available from internet. My program is using following jar files:

poi-ooxml-3.5-beta4.jar,
openxml4j-1.0-beta.jar

I have done following imports:

import java.io.*;

import java.io.File;

//import org.apache.poi.ss.usermodel.Workbook;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.usermodel.WorkbookFactory;

//import org.apache.poi.ss.usermodel.Cell;

//import org.apache.poi.ss.usermodel.CellType;

//import org.apache.poi.ss.usermodel.Row;import org.apache.poi.xssf.usermodel.XSSFSheet;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;

import java.util.*;

I am getting error in the following code:

Java:
public void readExcel()  {

        String FILE_PATH = "D:\\book1.xlsx";

         Sheet sheet = null;[b]// Error[/b]

try {

    Workbook workbook = WorkbookFactory.create(new File(FILE_PATH));[b]//Error[/b]    //FileInputStream inputFS = new FileInputStream(FILE_PATH);

    //Workbook workbook = WorkbookFactory.create(inputFS);

    //sheet = workbook.getSheetAt(0);

}

catch (Exception e) {

}

For Sheet, I am getting following error messages:
cannot find symbol
symbol: class Sheet

For Workbook, I am getting following error messages:

cannot find symbol
symbol: class Workbook

no suitable method found for create (String)
method WorkbookFactory.create(POIFSFileSystem) is not applicable
(argument mismatch; String cannot be converted to POIFSFileSystem)
method WorkbookFactory.create(Package) is not applicable
(argument mismatch; String cannot be converted to Package)
method WorkbookFactory.create(InputStream) is not applicable
(argument mismatch; String cannot be converted to InputStream)

Some body please guide me.

Zulfi.
 
Technology news on Phys.org
It's a bit difficult based on what you posted. Could you put the first 10 lines of the error from the stack trace? Add System.out.println(e.printStackTrace()) in the catch block.
 
  • Like
Likes jim mcnamara
Alternatively, you could export them as csv files and then they are much easier to import.

What IDE are you using? If not you should download one like Netbeans, Eclipse or IntelliJ.

They would have helped you get your imports straight ie the package names to classes path.
 
  • Like
Likes harborsparrow
Hi Borg,
Thanks for your reply. There is no run-time error. I am getting compilation error on line:
Sheet sheet = null; ===========Line (a)

It says :

cannot find symbol. class Sheet

I am also getting error on line:
Workbook workbook = WorkbookFactory.create(new File(FILE_PATH));=============Line (b)
It says:
cannot find symbol symbol: class Workbook no suitable method found for create (String) method WorkbookFactory.create(POIFSFileSystem) is not applicable (argument mismatch; String cannot be converted to POIFSFileSystem) method WorkbookFactory.create(Package) is not applicable (argument mismatch; String cannot be converted to Package) method WorkbookFactory.create(InputStream) is not applicable (argument mismatch; String cannot be converted to InputStream)
I am using NetBeans and there is a red bulb at the beginning of Line(a) and Line (b) indicating errors.
Some body please guide me.
Zulfi.
 
Zulfi, I'm no expert on Java, and haven't written any Java code for about 30 years. Having said that, I don't think some of your import lines are correct, so you probably aren't loading the libraries that you need.

For example, you have this:
Java:
import org.apache.poi.xssf.usermodel.XSSFSheet;
In this documentation, http://poi.apache.org/apidocs/index.html, there's a list of the packages, and the one above isn't listed.
I believe that the package you want is org.apache.poi.xssf.usermodel. Some of the classes contained in this package are XSSFSheet, XSSFWorkbook, and a bunch of others.

It seems to me that your import lines are including the class names, but I don't think you should be doing this. The import sections should just be the packages (collections of classes and interfaces) you're importing, but should not also include the classes you want.

The link I included above shows the hierarchy of all of the apache.poi packages, and within each package are listed the classes and interfaces that make up each package.
 
  • Like
Likes harborsparrow and jim mcnamara
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 3 ·
Replies
3
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
5
Views
15K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K
  • · Replies 28 ·
Replies
28
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
15K
  • · Replies 5 ·
Replies
5
Views
3K