.xlsx Excel files in Java: Sheet & Workbook error

Click For Summary

Discussion Overview

The discussion revolves around accessing and manipulating .xlsx Excel files in Java using the Apache POI library. Participants are addressing compilation errors encountered in the provided code, specifically related to importing necessary classes and using the WorkbookFactory for creating a workbook from a file.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports compilation errors related to the 'Sheet' and 'Workbook' classes, indicating that the symbols cannot be found.
  • Another participant requests additional information, specifically the first 10 lines of the error stack trace, to better understand the issue.
  • A suggestion is made to export the Excel files as CSV for easier importation into Java.
  • One participant notes that the IDE being used (NetBeans) may help in resolving import issues and suggests checking the package names for correctness.
  • A later reply questions the correctness of the import statements, suggesting that the participant may not be loading the necessary libraries correctly and provides a link to the Apache POI documentation for reference.

Areas of Agreement / Disagreement

Participants express differing views on the correct import statements and the approach to resolving the compilation errors. There is no consensus on a single solution, and multiple suggestions are offered.

Contextual Notes

Participants have not resolved the specific compilation errors, and there are indications of potential misunderstandings regarding the proper use of import statements and the structure of the Apache POI library.

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   Reactions: 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   Reactions: 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   Reactions: harborsparrow and jim mcnamara

Similar threads

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