I am trying to read an Excel workbook (.xlsx
) but the program just halts when initializing a Workbook
. I'm not sure what's happening as it doesn't give any errors.
When I say halt, I mean the program just pauses. It's still running but I feel like it's stuck, not sure.
import java.io.File;
import java.io.FileInputStream;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelReader
{
private String excelFilePath;
private FileInputStream inputStream;
private Workbook workbook;
private Sheet sheet;
// Constructors
public ExcelReader() {
try {
// Get path to excel workbook and put in stream
excelFilePath = "/home/flow/project/mydata.xlsx";
inputStream = new FileInputStream(new File(excelFilePath));
// Get type of workbook (Excel 2003 or Excel 2007+)
workbook = getWorkbook();
} catch (Exception e) {
e.printStackTrace();
}
}
// Get type of workbook (Excel 2003 or Excel 2007+)
private Workbook getWorkbook() {
Workbook work = null;
try {
if(excelFilePath.endsWith("xlsx")) {
System.out.println("In firstIf"); // Prints this
work = new XSSFWorkbook(inputStream); // Gets stuck here
System.out.println("After XSSF"); // Never prints this
}
else if(excelFilePath.endsWith("xls")) {
work = new HSSFWorkbook(inputStream);
}
else
throw new IllegalArgumentException("The specified file is not an Excel file");
} catch (Exception e) {
e.printStackTrace();
}
return work;
}
}
What am I doing wrong? Why doesn't the program ever move on to the next line?
I also noticed halt, I mean the program just pauses on the line where XSSFWorkbook is getting initialized. I found out that a couple of jar files were missing in the class path. In my case the following jars were not in the class path :
curvesapi-1.03.jar
xmlbeans-2.6.0.jar
It is strange that Java run time didn't throw class not found exception. Instead Java runtime was hanging on XSSFWorkbook constructor.
all the jars in POI distribution should be added to class path
poi-3.14-20160307.jar
poi-excelant-3.14-20160307.jar
poi-ooxml-3.14-20160307.jar
poi-ooxml-schemas-3.14-20160307.jar
poi-scratchpad-3.14-20160307.jar
commons-codec-1.10.jar
commons-logging-1.2.jar
junit-4.12.jar
log4j-1.2.17.jar
curvesapi-1.03.jar
xmlbeans-2.6.0.jar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With