# Writing Data In Excel

**URL:** <https://forum.katalon.com/t/writing-data-in-excel/12898>\
**Category:** Web Testing\
**Tags:** web-testing, katalon-studio\
**Created:** [August 29, 2018, 5:45am UTC](https://forum.katalon.com/t/writing-data-in-excel/12898 "2018-08-29T05:45:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikhil\_kumar](https://avatars.discourse-cdn.com/v4/letter/n/6f9a4e/32.png) [@Nikhil\_kumar](https://forum.katalon.com/u/Nikhil_kumar)\
**Post date:** [August 29, 2018, 5:45am UTC](https://forum.katalon.com/t/writing-data-in-excel/12898/1 "2018-08-29T05:45:18Z")

</div>

I found nullPointerException during writing data in Excel using same code as java selenium..  
i.e java.lang.NullPointerException: Cannot invoke method createCell() on null object

I found some solution like during writing data in excel we need to put data in that cell in which we creating cell then only it overwrite and write data in other cell in that row only.

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [August 30, 2018, 7:42am UTC](https://forum.katalon.com/t/writing-data-in-excel/12898/2 "2018-08-30T07:42:32Z")

</div>

This code is suitable to write data into xlsx file.

\_ String excelFileName = “C:\\results.xls”;\_

\_ File src = new File(excelFileName);\_

\_ FileInputStream fis = new FileInputStream(src);\_

\_ HSSFWorkbook wb = new HSSFWorkbook(fis);\_

\_ HSSFSheet sheet = wb.getSheetAt(0);\_

\_ HSSFRow row = sheet.createRow(recordcount-1);\_

\_ WebUI.delay(2)\_

\_ HSSFCell cell = row.createCell(0);\_

\_ cell.setCellValue(file.getName());\_

\_ cell = row.createCell(1);\_

\_ cell.setCellValue(status);\_

\_ cell = row.createCell(2);\_

\_ cell.setCellValue(page);\_

\_ WebUI.delay(2)\_

\_ FileOutputStream fileOut = new FileOutputStream(excelFileName);\_

\_ wb.write(fileOut);\_

\_ fileOut.flush();\_

\_ fileOut.close();\_

---

<div class="post-metadata">

**Author:** ![Kenneth\_A\_Ramos\_Medi](https://avatars.discourse-cdn.com/v4/letter/k/4491bb/32.png) [@Kenneth\_A\_Ramos\_Medi](https://forum.katalon.com/u/Kenneth_A_Ramos_Medi)\
**Post date:** [August 30, 2018, 1:19pm UTC](https://forum.katalon.com/t/writing-data-in-excel/12898/3 "2018-08-30T13:19:51Z")

</div>

> [plaidshirtakos](https://forum.katalon.com/profile/3222/plaidshirtakos) said:
> 
> This code is suitable to write data into xlsx file.
> 
> \_ String excelFileName = “C:\\results.xls”;\_
> 
> \_ File src = new File(excelFileName);\_
> 
> \_ FileInputStream fis = new FileInputStream(src);\_
> 
> \_ HSSFWorkbook wb = new HSSFWorkbook(fis);\_
> 
> \_ HSSFSheet sheet = wb.getSheetAt(0);\_
> 
> \_ HSSFRow row = sheet.createRow(recordcount-1);\_
> 
> \_ WebUI.delay(2)\_
> 
> \_ HSSFCell cell = row.createCell(0);\_
> 
> \_ cell.setCellValue(file.getName());\_
> 
> \_ cell = row.createCell(1);\_
> 
> \_ cell.setCellValue(status);\_
> 
> \_ cell = row.createCell(2);\_
> 
> \_ cell.setCellValue(page);\_
> 
> \_ WebUI.delay(2)\_
> 
> \_ FileOutputStream fileOut = new FileOutputStream(excelFileName);\_
> 
> \_ wb.write(fileOut);\_
> 
> \_ fileOut.flush();\_
> 
> \_ fileOut.close();\_

we just write the plain code in the script of our test case or do we need to create a keyword, i have tried many keywords out there but all of them clean my whole spreadsheet and just write the new text katalon send.

---

<div class="post-metadata">

**Author:** ![plaidshirtakos](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/plaidshirtakos/32/9361_2.png) [@plaidshirtakos](https://forum.katalon.com/u/plaidshirtakos)\
**Post date:** [August 31, 2018, 8:15am UTC](https://forum.katalon.com/t/writing-data-in-excel/12898/4 "2018-08-31T08:15:19Z")

</div>

Why would you like to create new keyword? You can simply put that code to your test case.

---

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [September 1, 2018, 11:14pm UTC](https://forum.katalon.com/t/writing-data-in-excel/12898/5 "2018-09-01T23:14:26Z")

</div>

I have made another post:

> [@Multiple Test Cases in a Test Suite can share a Excel file to read/write data dynamically](http://forum.katalon.com/t/how-multiple-test-cases-in-a-test-suite-safely-write-data-into-an-excel-file/12988):
>
> I have made a demo project on GitHub: [GitHub - kazurayam/MultipleTestCasesSafelyWriteIntoExcelFile: [Katalon Studio] Writing Data in Excel](https://github.com/kazurayam/MultipleTestCasesSafelyWriteIntoExcelFile) ----------- Problems to solve I wanted to find out a solution to the following points: I want my Katalon Studio project to generate a single Excel file where my Test Cases store anonymous data. I want to name the Excel file in such format. If I start a Test Suite named TS\_a then it creates a file named TS\_afollowed by the timestamp when it was started. …

I hope this may help.

---

<div class="post-metadata">

**Author:** ![Timo\_Kuisma](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@Timo\_Kuisma](https://forum.katalon.com/u/Timo_Kuisma)\
**Post date:** [January 12, 2019, 10:58am UTC](https://forum.katalon.com/t/writing-data-in-excel/12898/6 "2019-01-12T10:58:18Z")

</div>

Hi,

one way to write to excel 🙂

TESTCASE  
List excel = new ArrayList ();  
excel.add(“First”);  
excel.add(“Second”);  
excel.add(“Third”);  
excel.add(“Fourth”);  
excel.add(“Fifth”);  
excel.add("");  
CustomKeywords.‘spreadsheet.WriteToFile.writeToExcel’(excel, 1)

KEYWORD  
import org.apache.poi.ss.usermodel.Workbook  
import com.kms.katalon.core.annotation.Keyword  
import bsh.org.objectweb.asm.Label

import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;  
import org.apache.poi.hssf.usermodel.HSSFCellStyle;  
import org.apache.poi.hssf.usermodel.HSSFDataFormat;  
import org.apache.poi.hssf.usermodel.HSSFRow;  
import org.apache.poi.hssf.usermodel.HSSFSheet;  
import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
import org.apache.poi.hssf.util.HSSFColor;

```
public void writeToExcel(List <String> excel, int rows){
	
	try {
		//create .xls and create a worksheet.
		FileOutputStream fos = new FileOutputStream("C:\\Users\\xxx\\Desktop\\data\\data2excel.xls");
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet worksheet = workbook.createSheet("SheetName");
		HSSFRow row;
		HSSFCell cell;
		HSSFCellStyle cellStyle;
		for (int i = 0;i < rows; i++){
			row = worksheet.createRow((short) i);
			for(int y = 0; y < excel.size(); y++){
				cell = row.createCell((short) y);
				cell.setCellValue(excel[y]);
				cellStyle = workbook.createCellStyle();
				cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
				cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
				cell.setCellStyle(cellStyle);
			}
		}
		//Save the workbook in .xls file
		workbook.write(fos);
		fos.flush();
		fos.close();
	} catch (FileNotFoundException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	}
	
}
```
