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