0% found this document useful (0 votes)
3 views

Testing - Apache POI

Apache POI is an open-source Java library for handling Microsoft Office documents, enabling users to read, write, and manipulate files such as Excel, Word, and PowerPoint. It supports both XLS and XLSX formats and provides various APIs, including HSSF, XSSF, and SXSSF for efficient processing of large files. The document also outlines common components, test scenarios, and methods for working with Excel files using Apache POI.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Testing - Apache POI

Apache POI is an open-source Java library for handling Microsoft Office documents, enabling users to read, write, and manipulate files such as Excel, Word, and PowerPoint. It supports both XLS and XLSX formats and provides various APIs, including HSSF, XSSF, and SXSSF for efficient processing of large files. The document also outlines common components, test scenarios, and methods for working with Excel files using Apache POI.

Uploaded by

abhimanyu thakur
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

====================================================

Topic: Apache POI


====================================================
1. Document prepared by: Rajat Verma
a. https://www.linkedin.com/in/rajat-v-3b0685128/
b. https://github.com/rajatt95
c. https://rajatt95.github.io/

------------------------------------------------------------------------------------------------------------------------------
================== Testing - Apache POI =====================
------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------
Apache POI:
1. Links:
a. https://poi.apache.org/
b. https://mvnrepository.com/artifact/org.apache.poi/poi
2. Apache POI is a Java library that is used to handle Microsoft Office Documents. Apache POI is
open source and can be used by JVM-based programming languages.
3. Apache POI is an API provided by Apache foundation which is a collection of different java
libraries.
4. This library gives the facility to read, write and manipulate different Microsoft files such as
excel sheets, power-point, and word files.
5. The Apache POI in Selenium is a widely used API for selenium data-driven testing.
6. It is a POI library written in Java that gives users an API for manipulating Microsoft
documents like .xls and .xlsx.
7. The Apache POI in Selenium is a widely used API for selenium data-driven testing. It is a POI
library written in Java that gives users an API for manipulating Microsoft documents like .xls
and .xlsx.
8. Apache POI is an open-source java library to create and manipulate various file formats
based on Microsoft Office. Using POI, one should be able to perform create, modify and
display/read operations on the following file formats. For Example, Java doesn’t provide
built-in support for working with excel files, so we need to look for open-source APIs for the
job.

1
-------------------------------------
Important features:
1. Apache POI provides stream-based processing, that is suitable for large files and requires
less memory.
2. Apache POI is able to handle both XLS and XLSX formats of spreadsheets.
3. Apache POI contains HSSF implementation for Excel ’97(-2007) file format i.e XLS.
4. Apache POI XSSF implementation should be used for Excel 2007 OOXML (.xlsx) file format.
5. Apache POI HSSF and XSSF API provide mechanisms to read, write or modify excel
spreadsheets.
6. Apache POI also provides SXSSF API which is an extension of XSSF to work with very large
excel sheets.
7. SXSSF API requires less memory and is suitable when working with very large spreadsheets
and heap memory is limited.
8. There are two models to choose from – the event model and the user model. The event
model requires less memory because the excel file is read in tokens and requires processing.
The user model is more object-oriented and easy to use.
9. Apache POI provides excellent support for additional excel features such as working with
Formulas, creating cell styles by filling colors and borders, fonts, headers and footers, data
validations, images, hyperlinks, etc.
-------------------------------------
Commonly used components of Apache POI:

1. HSSF (Horrible Spreadsheet Format): It is used to read and write .xls format of MS-Excel
files.
2. XSSF (XML Spreadsheet Format): It is used for the .xlsx file format of MS-Excel.
3. POIFS (Poor Obfuscation Implementation File System): This component is the basic
factor of all other POI elements. It is used to read different files explicitly.
4. HWPF (Horrible Word Processor Format): It is used to read and write doc extension files
of MS Word.
5. HSLF (Horrible Slide Layout Format): It is used for reading, creating, and editing
PowerPoint presentations.
-------------------------------------
Test scenario: Facebook Login
Test cases:
1. Blank value
2. Invalid data
3. Invalid username and valid password
4. A valid username and invalid password
5. A valid username and Valid password
-------------------------------------

2
-------------------------------------
Read Excel file:
1. Xls vs xlsx:
2. Selenium supports only web applications.
3. So, for reading/write Excel files in Selenium, we have to take help from a third-party API like
JXL and Apache POI.
4. Apache POI is an open-source API,
a. Written in Java.
b. Gives so much flexibility to read/write excel files.
c. It has so many predefined methods
d. It supports both Excel 2003 (.xls) and Excel 2007 (.xlsx) file formats
5. JXL does not support Excel 2007 (.xlsx) file format.
a. It only supports Excel 2003 (.xls) file format
-------------------------------------
Working with Apache POI:

Classes and Interfaces used in POI

XLS classes (2003) Interface XLSX classes (2007)

HSSFWorkbook Workbook XSSFWorkbook

HSSFSheet Sheet XSSFSheet

HSSFRow Row XSSFRow

HSSFCell Cell XSSFCell

-------------------------------------
Methods used to deal with Excel file data are as below:
1. getRow(rowNum).getCell(cellNum).getStringCellValue() -> Print the data of any cell
2. getLastRowNum() -> Return Last row number of Table present in Excel sheet, it works on
the basis of index
3. getFirstRowNum() -> Return first row number of Table present in Excel sheet, it works on
the basis of index
4. getRow(rowNum).getPhysicalNumOfCells() -> Return Total number of columns in a row
5. getPhysicalNumOfRows() -> Return the total number of rows present in Excel sheet
6. getRow(0).getLastCellNum() -> Return last cell number in a 0th row, it does not work on
the basis of indexing
-------------------------------------

3
-------------------------------------
1. Code Snippets:
a. Read Rows
b. Read Columns
c. Print all Cells
d. Write Data
e. Read Data
f. CopyData_XLS_To_NewSheet_AllData
g. CopyData_XLSX_To_NewSheet_AllData

-------------------------------------

4
-----------------------------------------------------------------------------------------------------------------------------
CODE - START
------------------------------------------------------------------------------------------------------------------------------

-------------------------------------

1. Read Rows:

-------------------------------------

5
-------------------------------------

2. Read Column:

-------------------------------------

6
-------------------------------------

3. Print all cells

-------------------------------------

7
-------------------------------------

4. Write Data

-------------------------------------

8
-------------------------------------

5. Read Data

-------------------------------------

9
-------------------------------------

6. CopyData_XLS_To_NewSheet_AllData

-------------------------------------

10
-------------------------------------

7. CopyData_XLSX_To_NewSheet_AllData

-------------------------------------

-----------------------------------------------------------------------------------------------------------------------------
CODE - END
------------------------------------------------------------------------------------------------------------------------------

=======================================================================

11
1. To connect:
a. https://www.linkedin.com/in/rajat-v-3b0685128/
b. https://github.com/rajatt95
c. https://rajatt95.github.io/

=======================================================================

12

You might also like