Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:39:10+00:00 2026-06-06T19:39:10+00:00

i want to read the 2010 excel file in java using apache poi api

  • 0

i want to read the 2010 excel file in java using apache poi api … but it gives me an error
Exception in thread “main” java.lang.IllegalArgumentException: Sheet index (0) is out of range (0..-1)
i am using xssf
but if i want to get the data from old format of the excel then it works fine by sing HSSF ..
i dont know what is going with XSSF..here is my code ..plz help me in it.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * This java program is used to read the data from a Excel file and display them
 * on the console output.
 * 
 * @author dhanago
 */
public class xssff {

    /** Creates a new instance of POIExcelReader */
    public xssff() {
    }

    /**
     * This method is used to display the Excel content to command line.
     * 
     * @param xlsPath
     */
    @SuppressWarnings("unchecked")
    public void displayFromExcel(String xlsPath) {
        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(xlsPath);
        } catch (FileNotFoundException e) {
            System.out.println("File not found in the specified path.");
            e.printStackTrace();
        }


        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet sheet = workBook.getSheetAt(1);
        Iterator<Row> rows = sheet.rowIterator();

        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();

            // display row number in the console.
            System.out.println();

            // once get a row its time to iterate through cells.
            Iterator<Cell> cells = row.cellIterator();

            while (cells.hasNext()) {
                XSSFCell cell = (XSSFCell) cells.next();

                /*
                 * Now we will get the cell type and display the values
                 * accordingly.
                 */
                switch (cell.getCellType()) {
                case XSSFCell.CELL_TYPE_NUMERIC: {

                    // cell type numeric.
                    System.out.print(cell.getNumericCellValue() + "\t\t\t");

                    break;
                }

                case XSSFCell.CELL_TYPE_STRING: {

                    // cell type string.
                    XSSFRichTextString richTextString = cell
                            .getRichStringCellValue();

                    System.out.print(richTextString.getString() + "\t\t\t");

                    break;
                }

                default: {

                    // types other than String and Numeric.
                    System.out.println("Type not supported.");

                    break;
                }
                }
            }
        }

    }

    /**
     * The main executable method to test displayFromExcel method.
     * 
     * @param args
     */
    public static void main(String[] args) {
        xssff poiExample = new xssff();
        String xlsPath = "c://temp//data.xlsx";

        poiExample.displayFromExcel(xlsPath);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:39:12+00:00Added an answer on June 6, 2026 at 7:39 pm

    Ok I’ve found your problem, you’re creating a new book with no sheets

    XSSFWorkbook workBook = new XSSFWorkbook();
    XSSFSheet sheet = workBook.getSheetAt(1);  //error here, the workBook book has NO sheets!
    

    You should create the book based in the InputStream

    XSSFWorkbook workBook = WorkbookFactory.create(new PushbackInputStream(inputStream));
    XSSFSheet sheet = workBook.getSheetAt(1);
    

    Or even easier, just pass the file name to create the book:

    XSSFWorkbook workBook = new XSSFWorkbook(xlsPath);
    XSSFSheet sheet = workBook.getSheetAt(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I can't understand this error I want read this XML file: <?xml version=1.0
I have a text file. I want read that file. But In that if
i want to read an html file by using jquery get function, replace some
Now i want to read any excel file that are given by the user
I have project in VS 2010 with XML file. I want to read it
I want to read a INI file from python, and I came up with
I want to read a file piece by piece. The file is split up
I want to read the pdf file from raw folder if devices have any
My local PC is installed with Windows7 64bit and Microsoft Excel 2010 But most
There's an INI file that I want to access and read information from. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.