Reputation: 348
I'm creating a Java application that I need it to read specific cells, the cells on the document will contain Strings and some Integers. Can you please assist, I found this sample code below, but now I'm stuck. I need to persist the data on the spreadsheet to the database. Thank You
InputStream inp = new FileInputStream("workbook.xls");
//InputStream inp = new FileInputStream("workbook.xlsx");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
Upvotes: 0
Views: 6330
Reputation: 326
I changed your code to a working sample:
InputStream inp = new FileInputStream("workbook.xls");
HSSFWorkbook wb = new HSSFWorkbook(inp);
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(2);
Cell cell = row.getCell(3);
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.println("string: " + cell.getStringCellValue());
}
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
System.out.println("numeric: " + cell.getNumericCellValue());
}
System.out.println("any: " + cell.toString());
I hope it helps!
Upvotes: 4