.xlsx Excel files in Java: Sheet & Workbook error

  • Java
  • Thread starter zak100
  • Start date
  • #1
zak100
Gold Member
434
10

Main Question or Discussion Point

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.
 

Answers and Replies

  • #2
Borg
Science Advisor
Gold Member
1,871
2,268
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.
 
  • #3
11,500
5,045
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.
 
  • #4
zak100
Gold Member
434
10
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
33,279
4,986
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.
 

Related Threads on .xlsx Excel files in Java: Sheet & Workbook error

  • Last Post
Replies
1
Views
14K
  • Last Post
Replies
2
Views
2K
Replies
10
Views
537
Replies
4
Views
7K
Replies
6
Views
902
Replies
3
Views
2K
  • Last Post
Replies
10
Views
11K
Replies
1
Views
587
Replies
1
Views
5K
  • Last Post
Replies
3
Views
2K
Top