interop - check if a sheet exists in excel -
how check if sheet exists in excel using interop. tried following throws comexception if not there.. there better way of finding out looking @ exception
worksheet sheet = null; sheets worksheets = some; sheet = (worksheet)worksheets.get_item("sheetname"); if(sheet!=null) { //do }
edit:
thanks input guys.
i wrote function
private dictionary<string, worksheet> getsheetsmap(sheets worksheets) { if (worksheets == null) throw new argumentnullexception("worksheets"); dictionary<string, worksheet> map = new dictionary<string, worksheet>(stringcomparer.currentcultureignorecase); foreach (worksheet s in worksheets) { map.add(s.name, s); } return map; }
and use below
dictionary<string, worksheet> sheetmap = getsheetsmap(worksheets); worksheet sheet = null; if (sheetmap.trygetvalue(extendedtemplatemanager.basicusertemplate, out sheet)) { //found it. } else { // not }
do have workbook object? if so, can iterate on workbook.sheets array , check name property of each sheet.
foreach (sheet sheet in workbook.sheets) { if (sheet.name.equals("sheetname")) { //do } }
Comments
Post a Comment