.xlsx Excel files in Java: Sheet & Workbook error

In summary, Zulfi says that you may not be loading the necessary libraries, and suggests that you look at the apache.poi package hierarchy and see which classes you need.
  • #1
zak100
462
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
  • #2
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
  • #3
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
  • #4
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.
 
  • #5
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

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

What is an .xlsx Excel file?

An .xlsx Excel file is a type of spreadsheet file that is created and used by Microsoft Excel. It contains data organized into rows and columns, and can also include charts, graphs, and formulas.

How can I open and read an .xlsx Excel file in Java?

You can use the Apache POI library to open and read an .xlsx Excel file in Java. This library provides classes and methods for working with Excel files, including the ability to read and write data to cells and manipulate the workbook and sheets.

Why am I getting a Sheet or Workbook error when working with an .xlsx Excel file in Java?

Sheet and Workbook errors can occur when there is an issue with the structure or data in the Excel file. This can be caused by incorrect formatting, missing data, or other issues. It is important to carefully check the file and ensure that it is properly formatted before trying to read or manipulate it in Java.

How can I handle errors when working with .xlsx Excel files in Java?

You can use try-catch blocks to catch any errors that may occur when working with .xlsx Excel files in Java. This will allow you to handle the errors and take appropriate actions, such as displaying an error message to the user or logging the error for further investigation.

Can I create and write to an .xlsx Excel file in Java?

Yes, you can use the Apache POI library to create and write to an .xlsx Excel file in Java. This allows you to dynamically generate Excel files with data from your Java application, making it a useful tool for data analysis and reporting.

Similar threads

Replies
1
Views
1K
Replies
4
Views
4K
Replies
1
Views
2K
Replies
1
Views
1K
Replies
4
Views
2K
Replies
1
Views
14K
Replies
5
Views
2K
Back
Top