Write Data To Excel Using Column Number
Write data to excel using column number will discuss about how we can write test data/test results to the excel sheet while automating any application using selenium web driver. We might get doubt that why we need to put the test results in excel. If an application need so much of data to be feed to test; then we will manage those data using external sources like text file, XML file and excel file etc… . Among these; Excel is very user friendly that we can organize the data very easily and we can read and write the data how we want. So, we mostly use excel to maintain the test data and results. In order to create a data driven framework then it is easy with using excel.
So, if you want to write data to excel; we need to use some third party API which are available. In those one of the famous is Apache POI. In this blog we will use Apache POI to write the data to the excel sheet. To interact with the excel, Apache POI given so many classes to interact with the excel and read and write the data. To read/write excel data we will use Java and Apache POI classes.
Below are the classes will use to read/write the data:
- FileInputStream – A FileInputStream is an inputstream for reading data from a File.
- FileOutputStream – A FileOutputStream is an outputstream for writing data to a File.
- XSSFWorkbook
- XSSFSheet
- XSSFRow
- XSSFCell
- XSSFFont
- XSSFCellStyle
Below are the methods will use to read/write the data which are available in the above classes:
- getSheet(“sheetName”) – Get sheet with the given name
- getLastCellNum() – Get the index of the last cell contained in the row Plus one as index starts from ZERO
- setCellValue() – Set the value of the cell
- getRow(int) – Returns the row.
- getCell(int) – Get the cell representing a given column
Below is the sample Excel File:
We can download the Apache POI Jar files from this link and can find the screenshots below for the same.
Points to Remember:
- Row index starts from ZERO
- Column index starts from ZERO
- Extension of excel file is ‘.xlsx’ from Excel 2007
Now we will write a sample stand alone program that will write the data to the excel:
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.xssf.usermodel.*; import java.io.FileInputStream; import java.io.FileOutputStream; public class WriteDataToExcel { public static void main(String args[]) throws Exception { FileInputStream fis = new FileInputStream("/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx"); FileOutputStream fos = null; XSSFWorkbook workbook = new XSSFWorkbook(fis); XSSFSheet sheet = workbook.getSheet("Credentials"); XSSFRow row = null; XSSFCell cell = null; XSSFFont font = workbook.createFont(); XSSFCellStyle style = workbook.createCellStyle(); row = sheet.getRow(1); if(row == null) row = sheet.createRow(1); cell = row.getCell(4); if(cell == null) cell = row.createCell(4); font.setFontName("Comic Sans MS"); font.setFontHeight(14.0); font.setBold(true); font.setColor(HSSFColor.WHITE.index); style.setFont(font); style.setFillForegroundColor(HSSFColor.GREEN.index); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); cell.setCellStyle(style); cell.setCellValue("PASS"); fos = new FileOutputStream("/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx"); workbook.write(fos); fos.close(); } }
In the above program we have hard-coded the row and column numbers in the program. But for the best practice we will create a util method and will call that method where ever we need. For this we will write 2 classes, one will have the actual logic to read/write the excel data and another class will call this method.
Below is the excel read/write data util class. In this we will write a method called “setCellData()” and it will accept 4 parameters called sheetName, columnNumber, rowNumber and value which is written to the excel.
import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; public class ExcelApiTest { public FileInputStream fis = null; public FileOutputStream fos = null; public XSSFWorkbook workbook = null; public XSSFSheet sheet = null; public XSSFRow row = null; public XSSFCell cell = null; String xlFilePath; public ExcelApiTest(String xlFilePath) throws Exception { this.xlFilePath = xlFilePath; fis = new FileInputStream(xlFilePath); workbook = new XSSFWorkbook(fis); fis.close(); } public boolean setCellData(String sheetName, int colNumber, int rowNum, String value) { try { sheet = workbook.getSheet(sheetName); row = sheet.getRow(rowNum); if(row==null) row = sheet.createRow(rowNum); cell = row.getCell(colNumber); if(cell == null) cell = row.createCell(colNumber); cell.setCellValue(value); fos = new FileOutputStream(xlFilePath); workbook.write(fos); fos.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } }
Below is a sample program which will call the method from the above program to write data to excel using column number:
public class WriteDataToExcelUsingUtilClass { public static void main(String args[]) throws Exception { ExcelApiTest eat = new ExcelApiTest("/Volumes/Krishna/Jar Files/poi-3.16-beta1/TestData.xlsx"); eat.setCellData("Credentials",4,1,"PASS"); } }
This way we will write data to the excel using column number.
Please watch youtube video for better and more understanding.