importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOException;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;publicclassReadExcel{publicstaticvoidmain(String[]args){try{FileInputStreamfis=newFileInputStream(newFile("D://Selenium//Workbook-1.xlsx"));XSSFWorkbookworkbook=newXSSFWorkbook(fis);XSSFSheetsheet=workbook.getSheetAt(0);introwcnt=sheet.getLastRowNum();for(inti=0;i<=rowcnt;i++){Rowr=sheet.getRow(i);for(intj=0;j<r.getLastCellNum();j++){System.out.print(r.getCell(j).toString()+" ");}System.out.println();}System.out.println("Number of Columns: "+sheet.getRow(0).getLastCellNum());System.out.println("Number of Rows: "+sheet.getLastRowNum());}catch(IOExceptione){e.printStackTrace();}}}
Output
ID Name
1.0 Thomson
2.0 Ronaldo
3.0 Thomson
Number of Columns: 2
Number of Rows: 3
How to read an Excel file using Apache POI
KW.java
importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.util.Iterator;importorg.apache.poi.ss.usermodel.Cell;importorg.apache.poi.ss.usermodel.Row;importorg.apache.poi.xssf.usermodel.XSSFSheet;importorg.apache.poi.xssf.usermodel.XSSFWorkbook;publicclassReadExcel{publicstaticvoidmain(String[]args){try{FileInputStreamfis=newFileInputStream(newFile("D://Selenium//Workbook-1.xlsx"));XSSFWorkbookworkbook=newXSSFWorkbook(fis);XSSFSheetsheet=workbook.getSheetAt(0);Iterator<Row>ri=sheet.iterator();while(ri.hasNext()){Rowr=ri.next();Iterator<Cell>ci=r.cellIterator();while(ci.hasNext()){Cellc=ci.next();switch(c.getCellType()){caseSTRING:System.out.print(c.getStringCellValue()+" ");break;caseNUMERIC:System.out.print(c.getNumericCellValue()+" ");break;caseBOOLEAN:System.out.print(c.getBooleanCellValue()+" ");break;}}System.out.println();}System.out.println("Number of Columns: "+sheet.getRow(0).getLastCellNum());System.out.println("Number of Rows: "+sheet.getLastRowNum());}catch(IOExceptione){e.printStackTrace();}}}
Output
ID Name
1.0 Thomson
2.0 Ronaldo
3.0 Thomson
Number of Columns: 2
Number of Rows: 3
Dear User, Thank you for visitng Kodingwindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.
Dear User, We are grateful for your interest in the Kodingwindow Android app. We intend to soon make it available on the Google Play Store. Currently, Android versions 10 and up require manual apk installations. Would you like to get the app?