how do you get data from excel

How do you do test using excel files in Java?
I use Apache POI libraries to read and write from excel file, 
I add the Apache POI dependencies to my pom file.
In order to connect I use following classes.
 	-FileInputStream from Java. it is used to create connection to the file.
    We pass the file path as constructor to it.
 	-WorkBook is a class that represents the excel file.  
    We create Workbook object using the FileInputStream object.
 	-Sheet represents a single sheet from the excel file.
    We create sheet using Workbook object. We can create 
    worksheet using the 0 based index.

   public String readExcel(String path, String sheetName, 
                                                 int rowNum, int colNum) {
        try {
            FileInputStream file = new FileInputStream(path);
            Workbook book = WorkbookFactory.create(file);
            Sheet sheet = book.getSheet(sheetName);
            Row row = sheet.getRow(rowNum);
	    	Cell cell = row.getCell(colNum);
            String cellData = cell.toString();
            return cellData;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

How to get row and column numbers:
    int rowCount = sheet.getLastRowNum()+1; ==> why we add '+1'?
                                   Because row num starts from 0.
    int colCount = sheet.getRow(0).getLastCellNum();
    String sheetName = workSheet.getSheetName();

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source