shiva_ranabhat
9 years agoContributor
Solved
how to check if sheet in excel driver exist or not
Hi,
I want to check if Sheet1 exists or not. Sometimes 'Sheet1' is named as Error in our application, and hence is an actual error in export. Even though file is exported correctly.
Attempt:
var excelDDT= DDT.ExcelDriver(ProjectSuite.Variables.PlanExportDirectory, "Sheet1", true); if(excelDDT.){ Log.Message("excel DDT exists"); } else{ Log.Error("Exported file contains error"); } while(!DDT.CurrentDriver.EOF()){ //Test Log.message(DDT.CurrentDriver.Value(0)); } // Close driver DDT.CloseDriver(DDT.CurrentDriver.Name);
It throws Jscript error on first line because Sheet1 doesn't exist.
Thanks in advance,
Shiva
As Alex said, you can use Excel's COM object, Excel.Application for that. Something along the lines of:
var oExcel = Sys.OleObject("Excel.Application"); var oWb = oExcel.Workbooks.Open("C:\\MyFile.xls");
var oSheet = null; try { oSheet = oWb.Sheets("Sheet1"); } catch (e) {} if (oSheet != null) { Log.Message("The specified sheet exists."); } else { Log.Error("The specified sheet does not exists."); } oExcel.Quit();