Reputation: 2311
I am trying to read an xlsx file but I am getting the error "Work book can not be resolved a type". How to solve the same?
InputStream input = new BufferedInputStream(
new FileInputStream("F://myFile.xlsx"));
POIFSFileSystem fs = new POIFSFileSystem( input );
Workbook wb1 = WorkbookFactory.create(new File(fs)); // error is here
HSSFSheet sheet = wb.getSheetAt(0);
Iterator rows = sheet.rowIterator();
while( rows.hasNext() ) {
HSSFRow row = (HSSFRow) rows.next();
System.out.println("\n");
Iterator cells = row.cellIterator();
Upvotes: 1
Views: 10805
Reputation: 48326
Firstly, you need to have all the appropriate Apache POI component jars and their dependencies on your classpath. Secondly, you then need to import the POI classes.
Once you've downloaded Apache POI, and added the components to your classpath, your code would then look something like:
import org.apache.poi.ss.usermodel.*;
public class POIExample {
public static void main(String[] args) {
File f = new File("F://myFile.xlsx"));
Workbook wb1 = WorkbookFactory.create(f);
Sheet sheet = wb.getSheetAt(0);
for (Row row : sheet) {
System.out.println("\n");
for (Cell cell : row) {
System.out.println("Found Cell");
}
}
}
}
Upvotes: 0
Reputation: 85781
Java cannot read Excel files directly. You need a third party library to accomplish this. From the given code, looks like you need to add the libraries from Apache POI, specifically POI-HSSF and POI-XSSF.
Upvotes: 1