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

AI Thread 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
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Back
Top