Forum Stats

  • 3,827,682 Users
  • 2,260,803 Discussions
  • 7,897,336 Comments

Discussions

How to read .xlsx Excel files in Java: can't find Sheet and Workbook

User_AYF65
User_AYF65 Member Posts: 135 Red Ribbon
edited Sep 1, 2018 5:52PM in New To Java

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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">java.io.*;</span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">java.io.File;</span>

<span style="font-size: 10.0pt;">//import org.apache.poi.ss.usermodel.Workbook;</span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">org.apache.poi.ss.usermodel.*;</span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">org.apache.poi.ss.usermodel.WorkbookFactory;</span>

<span style="font-size: 10.0pt;">//import org.apache.poi.ss.usermodel.Cell;</span>

<span style="font-size: 10.0pt;">//import org.apache.poi.ss.usermodel.CellType;</span>

<span style="font-size: 10.0pt;">//import org.apache.poi.ss.usermodel.Row;</span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">org.apache.poi.xssf.usermodel.XSSFSheet; </span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">org.apache.poi.xssf.usermodel.XSSFWorkbook;</span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">javax.swing.*;</span>

<span style="font-size: 10.0pt;">import</span> <span style="font-size: 10.0pt;">java.util.*;</span>

I am getting compilation error in the following code (because NetBeans is indicating red bulbs on Line 3 and Line 5):

?

1

2

3

4

5

6

7

8

9

10

11

12

<span style="font-size: 10.0pt;">public</span> <span style="font-size: 10.0pt;">void</span> <span style="font-size: 10.0pt;">readExcel()  {</span>

<span style="font-size: 10.0pt;">        String FILE_PATH = "D:\\book1.xlsx";</span>

<span style="font-size: 10.0pt;">         Sheet sheet = null;                                                      <strong>[b]// Error[/b]</strong></span>

<span style="font-size: 10.0pt;">try</span> <span style="font-size: 10.0pt;">{</span>

<span style="font-size: 10.0pt;">    Workbook workbook = WorkbookFactory.create(new</span> <span style="font-size: 10.0pt;">File(FILE_PATH));               <strong>[b]//Error[/b]</strong></span>

<span style="font-size: 10.0pt;">    //FileInputStream inputFS = new FileInputStream(FILE_PATH);</span>

<span style="font-size: 10.0pt;">    //Workbook workbook = WorkbookFactory.create(inputFS);</span>

<span style="font-size: 10.0pt;">    //sheet = workbook.getSheetAt(0);</span>

<span style="font-size: 10.0pt;">}</span>

<span style="font-size: 10.0pt;">catch</span> <span style="font-size: 10.0pt;">(Exception e) {</span>

<span style="font-size: 10.0pt;">}</span>



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

  • OrhanCemalDuru
    OrhanCemalDuru Member Posts: 6
    edited Jul 31, 2018 10:35AM

    You need to use the XSSFWorkbook object to create a new workbook. (org.apache.poi.xssf.usermodel)

    workbook = new XSSFWorkbook(new FileInputStream(FILENAME));

    Then you can assign Sheets accordingly, using getSheetAt() method.

  • dvohra21
    dvohra21 Member Posts: 14,579 Gold Crown
    edited Sep 1, 2018 5:52PM

    An FAQ, please refer one of the several blogs on the topic.

    "Using Apache POI to Read Excel"

    "Introduction to the Apache POI Library"

    "Working with Microsoft Excel in Java"

This discussion has been closed.