Geethanjali
5 years agoOccasional Contributor
Getting "Java Run time Error" when writing to Excel
Hi,
Am trying to write my result back to an excel file. Am using the code as below .But am getting "Java Run time error". Any help here would be really helpful.
let Excel = Sys.OleObject("Excel.Application");
var results = Excel.Workbooks.Open("Excel file Path name");
var sheet = results.Sheets.Item("Sheet1");
Excel.Cells.$set("Item" , 0, 18, "Generate Claim Number");
I have also tried
sheet.Cells.$set("Item" , 0, 18, "Generate Claim Number");
sheet.Cells.Item(0, 18).Value = "Generate Claim Number";
A more complete working sample just for you
Beware of indexes of Line and Column starting at 0.
function writeExcel(Valeur, Line, Column, ExcelFile, SheetName) { let excelObject, excelWorkbookObj, excelMainSheet; try { excelObject = Sys.OleObject("Excel.Application"); if (excelObject == null) throw Error("Excel n'est pas installé !"); excelObject.Visible = false; excelObject.DisplayAlerts = false; excelWorkbookObj = excelObject.Workbooks.Open(ExcelFile); excelMainSheet = excelWorkbookObj.Sheets.item(SheetName); excelMainSheet.Cells.Item(Line, Column).Value2 = Valeur; excelObject.Application.ActiveWorkbook.Save; } catch(e) { Log.Message("Erreur durant la mise à jour Excel", e.message, pmHigher); } finally { if (excelObject != null) { excelObject.Application.ActiveWorkbook.Close; excelObject.Quit; excelObject = null; excelWorkbookObj = null; excelMainSheet = null; } } }