Reading data from .xls and .xlsx file using Apache poi
Hi CEans ,
Was doing a POC for reading a Spreadsheet in .xls and .xlsx format using Apache POI.
Thought might be useful to you guys.
The code is the most basic version of Reading from either of the formats.Its applications are endless.let me know if there is some specific requirement.
Was doing a POC for reading a Spreadsheet in .xls and .xlsx format using Apache POI.
Thought might be useful to you guys.
The code is the most basic version of Reading from either of the formats.Its applications are endless.let me know if there is some specific requirement.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; public class ExcelRead { public static void main(String ar[]) { String fname = "B:\\AnkurTostudy\\poi-3.8\\TestSheet.xlsx"; System.out.println(fname); Workbook workbook = null; Sheet sheet = null; try { InputStream in = new FileInputStream(fname); try { workbook = WorkbookFactory.create(in); sheet = workbook.getSheetAt(0); int noOfRows = sheet.getPhysicalNumberOfRows(); for (int rownum = 0; rownum < noOfRows; rownum++) { Row row = sheet.getRow(rownum); int noOfColumns = row.getLastCellNum(); for (int colnum = 0; colnum < noOfColumns; colnum++) { Cell cell = row.getCell(colnum); switch (cell.getCellType()) { case Cell.CELL_TYPE_STRING: System.out.println(cell.getStringCellValue()); break; case Cell.CELL_TYPE_NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { System.out.println(cell.getDateCellValue()); } else { System.out.println(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_BOOLEAN: System.out.println(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: System.out.println(cell.getCellFormula()); break; default: System.out.println("In Default"); } } } } catch (InvalidFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Here is how the Build Path looks like.

0