Quality Testing

Quality is delighting customers

How to create a method to set cell value in a xlsx file?

How to create a method to set cell value in a xlsx file?

Views: 95

Reply to This

Replies to This Discussion

Can you please try this below code:

public void SetData(int rowNum, int colNum, String sheetName, String setValue)
{
try
{
FileInputStream fis = new FileInputStream(new File("C://data.xlsx")));
@SuppressWarnings("resource")
XSSFWorkbook workbook = new XSSFWorkbook (fis);
XSSFSheet sheet = workbook.getSheet(sheetName);


XSSFRow row = sheet.getRow(rowNum);
XSSFCell col = row.createCell(colNum);
col.setCellValue(setValue);

fis.close();

FileOutputStream fos =new FileOutputStream(new File("C://data.xlsx")));
workbook.write(fos);
fos.close();
}catch(Exception e)
{
e.printStackTrace();
}

}

Selenium doesn't have built-in support to read and write the excel files, so most of the software testing solutions recommend using the Apache Poi API to read/write data from the excel files.

First, you will need to add Apache POI jars into your project, or if you are using maven you can also add maven dependency into your POM file.  

There are three API you will be needing poipoi-ooxmlpoi-ooxml-schemas.

Below is the JAVA file to Write into the excel files sheet cells,

Java file containing a function to set values: Writing_data.java

Java file using the above class method to write the data into excel file sheet: Test_SetData_xlsx.java

To simply write data into excel files using the above-provided class files. Create a new folder under the c: with the name 'Data' and place both the files under that folder.

records.xlsx

Please do let me know in case of any doubts or issues.

Thanks 

Anand Singh

RSS

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2019   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service