Remember:-For the .XLSX file type of Excel we Always use XSSFWorkbook()
********************CODE*********************
package aveditor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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.xssf.usermodel.XSSFWorkbook;
public class ReadFromExcel {
public static void main(String[] args) throws Exception {
// Giving the File Location or Path
File file=new File("C:\\Users\\oracle\\Music\\WorkBook.xlsx");
// Opening the File as InputStream mean we can read only
FileInputStream input=new FileInputStream(file);
// Creating workBook as Type of XSSF for .xlsx type
Workbook workbook=new XSSFWorkbook(input);
// Getting the Sheet name i.e "Sheet1"
Sheet sheet=workbook.getSheet("Sheet1");
// Counting the total Number of Rows
int row=sheet.getLastRowNum();
int i;
// This loops is for Row
for(i=0;i<row+1;i++)
// For each row getting the Column
Row rowCount=sheet.getRow(i);
// This loop is for Column
for(int j=0;j<rowCount.getLastCellNum();j++)
{
// Printing the Row and Columm cell value type String
System.out.println(rowCount.getCell(j).getStringCellValue()+"|");
}
}
}