Melinda Lakner said:
I would like to get different texts and write them into excel into different cells, but now only the last appears not all. I don’t know why.
I needed to write data to excel. I created Custom Keyword for that.
Under Keywords right click and select NEW → PACKAGE.
Name it writeToExcel (or however you want)
After that right click your package and select NEW → KEYWORD.
Name it WriteExcel (or however you want).
Inside your new keyword add this code (my code has package name writeToExcel and keyword name WriteExcel, if you change yours then you need to modify my code):
package writeToExcel
import org.apache.poi.ss.usermodel.Cell
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import com.kms.katalon.core.annotation.Keyword
public class WriteExcel {
@Keyword
def void writeToExcel(int iRow, int iCell, String iText ){
FileInputStream file = new FileInputStream (new File(“C:\\YOUR_LOCATION_OFFILE\\FILE_NAME.xlsx”))
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheet(“YOUR_SHEET_NAME”)
//Write data to excel’
Row oRow;
oRow = sheet.getRow(iRow);
if(oRow == null){
sheet.createRow(iRow);
oRow = sheet.getRow(iRow);
}
Cell oCell;
oCell = oRow.getCell(iCell - 1);
if(oCell == null ){
oRow.createCell(iCell - 1);
oCell = oRow.getCell(iCell - 1);
}
oCell.setCellValue(iText);
FileOutputStream outFile =new FileOutputStream(new File(“C:\\YOUR_LOCATION_OFFILE\\FILE_NAME.xlsx”));
workbook.write(outFile);
outFile.close();
}
}
You call it like this:
CustomKeywords.‘writeToExcel.WriteExcel.writeToExcel’(your_Row, your_cell, ‘your_text’)
Remember to close your EXCEL file before you run your script or it will be read-only and you want be able to write.