Forum Discussion

robert_L's avatar
robert_L
Occasional Contributor
12 years ago

how do I create an new excel workbook from JScript?

Hello,





There is a SmartBear TestComplete "How To" article on how to write data to MS Excel.  But it assumes the excel file already exists.



How can one create a new excel file from the TestComplete script (in JScript)?



I've pasted the "writing data to MS Excel" JScript code from your website here for reference.  In it, after the Sys.OleObject line, it goes straight to

"var book = app.Workbooks.Open(fname);"



I need somehow to create this file fname (in a specific directory) beforehand, in the script.



Thanks, in advance, for any help,

Rob





function Main()




{




var fileName = "<Path_To_The_File>";




var sheetName = "Sheet1";








WriteExcelSheet(fileName, sheetName)




}








function WriteExcelSheet(fname, sheetName)




{




var maxcol = 5, maxrow = 5;








var app = Sys.OleObject("Excel.Application");








var book = app.Workbooks.Open(fname);




var sheet = book.Sheets(sheetName);




app.DisplayAlerts = false;








// Write an index of the current row and column to a cell




var rowCount = sheet.UsedRange.Rows.Count + 1;




for(var row = rowCount; row < rowCount + maxrow; row++)




for(var col = 1; col <= maxcol; col++)




sheet.Cells(row, col) = row + ", " + col;








book.Save();




app.Quit();




}