0

I need to create a HSSFWorkbook or an XSSFWorkbook obect, depending on what i read as file extension and then be able to proceed operations and stuff with the object created. How can i make the Object visibile outside the IF statement, so that i can use it "globally"?

I've tried with a method, but we know that a method can only return ONE object type and i am dealing with 2 possible object types output (HSSF/XSSF Workbook)

String excelFilePath = "D://"; //path
String fileName = "BetsTable"; //filename
String extension = "xls"; //extension
String completePath = excelFilePath + fileName + "." + extension; //fullpath

FileInputStream inputStream = new FileInputStream(new 
File(completePath));

if(extension == "xls") { 
    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);                      
}
if(extension == "xlsx") {
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);                      
}

Sheet firstSheet = workbook.getSheetAt(0); // !!! WORKBOOK IS NOW NOT 
"USABLE"

I expect nothing, since i know how the scope works in this case, but i need a way to fulfill this option

  • Possible duplicate of [Block scope variables](https://stackoverflow.com/questions/20499554/block-scope-variables) –  Aug 14 '19 at 09:36

3 Answers3

3

In short, no.

But, you can keep a Sheet outside the inner scope, you just need to define it in the scope you need it:

Sheet sheet = null;
if(extension == "xls") { 
    HSSFWorkbook workbook = new HSSFWorkbook(inputStream);     
    sheet = workbook.getSheetAt(0);                 
}
if(extension == "xlsx") {
    XSSFWorkbook workbook = new XSSFWorkbook(inputStream);       
    sheet = workbook.getSheetAt(0);               
}

// sheet is accessible from here
doSomething(sheet);
cameron1024
  • 9,083
  • 2
  • 16
  • 36
  • thats a very smart option, since i should only deal with the sheet. I will try that out! Thanks a lot!! – Lorenzo Tusorella Aug 14 '19 at 09:42
  • 1
    This is a good solution. Basically you don't need the Workbook object anymore outside the if statements. If you need them you should find the "Parent class" of both Workbook classes. Let's say its a class called `Workbook`. Then before entering the if statements you define the variable `Workbook workbook = null` and inside your ifs you instantiate them `workbook= new HSSFWorkbook..`. Then your `workbook` variable is known outside the ifs but carries different implementations, depending on the ifs. – GameDroids Aug 14 '19 at 09:44
  • I looked for a `Workbook` superclass for `HSSFWorkbook` and `XSSFWorkbook` but it seems like there isn't one like this :/ . However this would also work in other contexts – cameron1024 Aug 14 '19 at 10:17
  • @cameron1024: did you look in the javadoc? [Workbook interface](https://poi.apache.org/apidocs/dev/org/apache/poi/ss/usermodel/Workbook.html) – jhamon Aug 14 '19 at 12:35
1

It's possible if both variables shares a common type, and if you don't expect to use a method which is only in one of those classes.

In your case, both classes implements the interface Workbook and that's where the method getSheetAt(int) is defined.

Workbook workbook = null;
if(extension == "xls") { // that's bad, use equals
    workbook = new HSSFWorkbook(inputStream);                      
}
if(extension == "xlsx") { // that's bad, use equals
    workbook = new XSSFWorkbook(inputStream);                      
}
Sheet firstSheet = workbook.getSheetAt(0);

Warning: This code will throw a NPE if the extension is neither "xls" or "xlsx"

jhamon
  • 3,603
  • 4
  • 26
  • 37
1

In general you shouldn't use == for String comparisons. Use equals.

But concerning your problem: It is not possible. When you create the variable within a block it is not accessible outside the block.

In terms of clean code it might be good practice to create an own method for it. Then you also don't have the problem any more.

//your code starts here
    String excelFilePath = "D://"; //path
    String fileName = "BetsTable"; //filename
    String extension = "xls"; //extension
    String completePath = excelFilePath + fileName + "." + extension; //fullpath
    FileInputStream inputStream = new FileInputStream(new File(completePath));
    Sheet firstSheet = getFirstSheet(inputStream, extension);
//your code ends here (don't forget to check if firstSheet is null)

    private static Sheet getFirstSheet(FileInputStream inputStream, String extension) throws IOException {
        if("xls".equals(extension)) {
            return new HSSFWorkbook(inputStream).getSheetAt(0);
        } else if("xlsx".equals(extension)) {
            return new XSSFWorkbook(inputStream).getSheetAt(0);
        } else {
            return null;
        }
    }
MaS
  • 393
  • 3
  • 17