For reading files from the.xlsx file we need the following.jar files to be imported to the Build Path.Downloads the followings and import the version. Poi-ooxml-3.11-beta2.jar poi-ooxml-schemas-3.11-beta2.jar xmlbeans-2.6.0.jar stax-api-1.0.1.jar. If you are not using Maven then add following JAR files in your poi-3.11-beta2.jar commons-codec. To Read and Write excel files in Selenium we have to take help of third party API like JXL and Apache POI. To read or write an Excel,Apache provides a very famous library POI. This library is capable enough to read and write both XLS and XLSX file format of Excel. Let’s see how to Read excel files using Apache POI in Selenium WebDriver: Assuming that you have already downloaded and configured Apache POI jars in your project. If not, follow the below steps: Step 1– Download Apache POI jar file. Download link of Apache POI Jars.
In the last post, we have already discussed how to read excel data using POIbut we have missed one important point which can become the blocker for you. We can read numeric data from Excel using apache poi for Selenium webdriver.
Our test data contains numeric data, string data, and sometimes combination as well which is totally dynamic in nature. Missing protocol windows 10. so we need to create such library which will take care of dynamic data.
Read numeric data from Excel using apache poi
Program to read numeric data from Excel using apache poi
2 4 6 8 10 12 14 16 18 20 22 24 26 28 | import java.io.FileInputStream; import org.apache.poi.xssf.usermodel.XSSFWorkbook; { @Test { // return the path of local path of project XSSFWorkbook wb=newXSSFWorkbook(newFileInputStream(newFile(path+'/TestData/AppTestData.xlsx'))); // read numeric data intdata=(int)wb.getSheetAt(0).getRow(0).getCell(1).getNumericCellValue(); // print on console System.out.println('Data from Excel is >>> '+data); } |
Few observations while reading the different set of Data
- You need to make sure which kind of data you want to read.
- To read String value you can call getStringCellValue() method
- To read Numeric value you can call getNumericCellValue() which will return double value which we can typecast into int and then we can use in our test data.
You can check below screenshot which will explain what are the different type of methods which can help us to read data.
While implementing Data-driven frameworkyou have to make sure that data which is coming from excel should be used in script effectively.
The Same concept will be applied while writing to excel as well. While passing the data to excel you can call respective methods.
Let’s see the below screenshot for more details.
In above screenshot,you can see it will ask you to pass the specific data to write in excel.
Hope above post will help you to read the numeric data from excel.
If you have any doubt then let me know in comment section.
Excel files (spreadsheets) are widely used by people all over the world for various tasks related to organization, analysis, and storage of tabular data.
Since excel files are so common, we developers often encounter use-cases when we need to read data from an excel file or generate a report in excel format.
In this article, I’ll show you how to read excel files in Java using a very simple yet powerful open source library called Apache POI.
And in the next article, You’ll learn how to create and write to an excel file using Apache POI.
Poi For Xlsx Read In Selenium For Mac Download
Let’s get started!
Poi For Xlsx Read In Selenium For Mac Windows 10
Dependencies
Poi For Xlsx Read In Selenium For Mac Os
First of all, We need to add the required dependencies for including Apache POI in our project. If you use maven, you need to add the following dependencies to your pom.xml
file - Gujarati font converter software, free download.
Maven
Gradle
If you use gradle then you can add the following to your build.gradle
file
Poi For Xlsx Read In Selenium For Mac Windows 7
The first dependency poi
is used to work with the old Microsoft’s binary file format for excel. These file formats have .xls
extension.
The second dependency poi-ooxml
Free online graphing calculator ti 84 for mac. is used to work with the newer XML based file format. These file formats have .xlsx
extension.
Sample Excel file that We’ll read
Following is a sample excel file that we’ll read in our code. It is created using Google Sheets and has .xlsx
extension.
Note that, Although the sample file is of the newer XML based file format (.xlsx
). The code that we’ll write will work with both types of file formats - .xls
and .xlsx
Apache POI terminologies
Apache POI excel library revolves around following four key interfaces -
Workbook: A workbook is the high-level representation of a Spreadsheet.
Sheet: A workbook may contain many sheets. The sample excel file that we looked at in the previous section has two sheets -
Employee
andDepartment
Row: As the name suggests, It represents a row in the spreadsheet.
Cell: A cell represents a column in the spreadsheet.
Poi For Xlsx Read In Selenium For Mac Osx
HSSF and XSSF implementations -
Apache POI library consists of two different implementations for all the above interfaces.
HSSF (Horrible SpreadSheet Format): HSSF implementations of POI’s high-level interfaces like
HSSFWorkbook
,HSSFSheet
,HSSFRow
andHSSFCell
are used to work with excel files of the older binary file format -.xls
XSSF (XML SpreadSheet Format): XSSF implementations are used to work with the newer XML based file format -
.xlsx
.
Program to Read an excel file using Apache POI
The following program shows you how to read an excel file using Apache POI. Since we’re not using any file format specific POI classes, the program will work for both types of file formats - .xls
and .xlsx
.
The program shows three different ways of iterating over sheets, rows, and columns in the excel file -
Note that we’re not even using the concrete classes like HSSFWorkbook
and XSSFWorkbook
to create an instance of the Workbook. We’re creating the workbook using a WorkbookFactory
instead. This makes our program format independent and it works for both types of files - .xls
and .xlsx
.
The program shows three different ways to iterate over sheets, rows, and columns. I prefer the Java 8 forEach loop with a lambda expression. You may use whichever method you like.
Note that, I’ve used a DataFormatter
to format and get each cell’s value as String.
Retrieving Cell values by CellType
Instead of using a DataFormatter
to format and get each cell’s value as String regardless of the Cell type, You may check each cell’s type and then retrieve its value using various type-specific methods like this -
You may now call the above method in the main program to print each cell’s value -
Conclusion
That’s all folks! In this article, You learned how to read excel files in Java using Apache POI library. You can find the entire source code on the github repository.
Also, Don’t forget to check out the next article to learn how to create and write to an excel file using Apache POI
Thank you for reading. Until next time!