Forum Discussion

DIVYAMOHAN04's avatar
DIVYAMOHAN04
New Contributor
6 years ago

Doubt

CAN ANYONE OF YOU HELP ME IN DEMOS FOR THE BELOW MENTIONED TOPICS

Demo excel (POI.jar) , Read and Write to file(txt,excel) , JDBC connections  , Contains(complex) and more demos., Github and Jenkins, REST SV

 

Can anyone check whether the below code is correct for reading the data from excel and write it back to excel

 

//read data from excel

//helps to fetch all the files
import jxl.*
//to read from excel
Workbook wb= Workbook.getWorkbook(new File("C:\\Users\\divya_mohan04\\Desktop\\DataToBeRead.xls"));
//either index of sheet of name of sheet within double quotes can be given//using object of workbook we are accesing the values
Sheet sh = wb.getSheet("Sheet1")
//how many data are there in excel//rc-row count//cc-column count
rc = sh.getRows();
cc = sh.getColumns();
log.info "Row Count" : rc
log.info "Row Count" : cc
for(i=1;i<rc;i++)
{
for(j=0;j<cc;j++)
{
def num1 = testRunner.testCase.getTestStepByName("Properties")
Cell var = sh.getCell(i,j)
log.info var.getContents()
}
}

 

 

 

 

//write data to excel

//In order to write anything we need to first create a writable workbook as below which creates the workbook object.
WritableWorkbook workbook = Workbook.createWorkbook(new File("c:\\Testing\\myfile.xls---give location"))
//We can also use the existing excel sheet and write the data into excel sheet.
//But before doing write operations we need to make sure to take the copy of the original file
//and then perform the write operations. The below code is the sample code.
Workbook workbook = Workbook.getWorkbook(new File("testSampleData.xls"));
WritableWorkbook workbookCopy= Workbook.createWorkbook(new File("testSampleData.xls"), workbook);
//Now access the sheet from the workbook which is copied from the original.
WritableSheet wSheet = workbookCopy.getSheet(0);
//now we have the worksheet-so now we have to pass the data into excel by mentioning the column number and row number
WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);
Label label = new Label(iColumnNumber, iRowNumber, strData);
wshTemp.addCell(label);

 

No RepliesBe the first to reply