I work on Intellij about Java project. Then I take an error in my try catch block about incompatible types. Here part of my code:
try (HSSFWorkbook workbook = new HSSFWorkbook(new POIFSFileSystem(new FileInputStream(System.getProperty("user.home") + "D:\\Users\\john\\Desktop\\RBBNCaseManagementTool\\src\\home\\data.xls")))) {
HSSFSheet filtersheet = workbook.getSheetAt(0);
int cellnum = filtersheet.getRow(0).getLastCellNum();
int lastRow = filtersheet.getLastRowNum();
HSSFCell cellVal1;
HSSFCell cellVal2;
HSSFCell cellVal3;
Is there any way to solve this error ?
The HSSFWorkbook class implements the Workbook interface.
In POI 4.x, the Workbook interface implements AutoCloseable and Closeable.
In POI 3.x from 3.11 onwards, the Workbook interface implements onlyCloseable.
(Prior to 3.11, Workbook apparently didn't even implement Closeable; see https://poi.apache.org/changes.html#3.11 and https://bz.apache.org/bugzilla/show_bug.cgi?id=56537)
So, if you want to use try with resources to manage an HSSFWorkbook instance, the simple solution is to upgrade to POI 4.0 or later.
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