Forum Discussion
I updated the code to the following.
Workbook Main = Workbook.getWorkbook(new File("C:/FileLocation/ExcelFile.xls"));
WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/FilLocation/ExcelFile2.xls"), ws);
System.out.println("Did excel file create?");
//create work sheet
WritableSheet workSheet = null;
workSheet = workbook.createSheet("Test Report" ,0);
SheetSettings sh = workSheet.getSettings();
//write to datasheet
workSheet.addCell(new jxl.write.Label(0,0,"User Name"));
workSheet.addCell(new jxl.write.Label(1,0,"Password"));
//write to the excel sheet
workbook.write();
//close the workbook
workbook.close();
Main.close();
It creates the file but still does not update the current file which is what I am looking for. Once again if anyone knows this, please let me know. I will update this as I go.
Thanks
- TanyaYatskovska9 years ago
Alumni
Hi Don,
I'm not familiar with Groovy very well. However, I would suggest that you check if a file exists first. After that, you can use createWorkbook or getWorkbook methods depending on the results of the previous step.
@Community, any other ideas?
I found a solution to the problem.
First I switched to Apache POI. It's similar to jxl but has more support and is constantly updated.
With POI, in order to update get your desired results (at least from what I'm seeing) you would have to upload the file you want to update and create a copy of the file. The reason this happens is because the system locks the first while. When I was doing this with jxl, the whole file would overwrite any data that was in the file and wouldn't output my desired results.
Here is some sample code that I found that works for me.
FileInputStream fsIP= new FileInputStream(new File("C:/FileLocation/Excel.xls")); //Read the spreadsheet that needs to be updated HSSFWorkbook wb = new HSSFWorkbook(fsIP); //Access the workbook HSSFSheet worksheet = wb.getSheetAt(6); //Access the worksheet, so that we can update / modify it. def response=context.expand('${RESTAPI#Response}') def i = (context.expand('${RESTAPI#Order}')).toInteger() def json = new JsonSlurper().parseText(response) a=json.AMT.toString() b=(json.Date.toString()).substring(0,10) c=json.Num.toString() //Cell cell = null; // declare a Cell object cell = worksheet.getRow(i).getCell(14); // Access the second cell in second row to update the value cell.setCellValue(a); // Get current cell value value and overwrite the value cell = worksheet.getRow(i).getCell(15); cell.setCellValue(b); cell = worksheet.getRow(i).getCell(16); cell.setCellValue(c); fsIP.close(); //Close the InputStream FileOutputStream output_file =new FileOutputStream(new File("C:/FileLocation/File2.xls")); //Open FileOutputStream to write updates wb.write(output_file); //write changes output_file.close(); //close the stream
I ran into another issue though. During a DataSource Loop, the file only writes it to one line when I have multiple. I will look for a solution and post it here once I find it for those who are looking for the same.
I'm new to Groovy and Ready API so I'm learning this as I go.
- TanyaYatskovska9 years ago
Alumni
That's great that you figure out the issue. Thanks for sharing the solution with us!