Anton Cagle
Anton Cagle

Reputation: 27

How to access XSSF content when no sheets are recognized

I am trying to read in an excel file that is produced by a web app. I can't get to any of the columns or rows, though, because the XSSF tool keeps telling me I have no worksheets. The single worksheet is called "MySheet", but when I search for it by name, I get a -1. When I search for the number of worksheets, it tells me I have 0. What I am doing wrong?

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class excelParser {

    private static final String FILE_NAME = "C:\\Users\\me\\Downloads\\output.xlsx";

    public static void main(String[] args) throws FileNotFoundException {


                if (new File(FILE_NAME).exists()){

                FileInputStream excelFile = new FileInputStream(new File(FILE_NAME));
                Workbook workbook = new XSSFWorkbook();
                //Sheet datatypeSheet = workbook.getSheet("MySheet");

                //System.out.println(datatypeSheet.getSheetName());
                System.out.println(workbook.getSheetIndex("MySheet"));
                }
                else System.out.println("file not found");
    }
}

Upvotes: 0

Views: 447

Answers (1)

Gyunesh Shefkedov
Gyunesh Shefkedov

Reputation: 181

The problem is that you are not using the file in your Workbook workbook = new XSSFWorkbook(); constructor. Try with Workbook workbook = new XSSFWorkbook(excelFile);

Upvotes: 1

Related Questions