How to check if Excel-workbook is empty?
-
I'm trying to write a method to find out if a workbook is empty or not.. I'm using Excel 2007 and Microsoft.Office.Interop.Excel.Workbook. If the workbook has content in more than one cell I'm able to detect this, but if I have an empty "workbookA" and a "workbookB" with content in cell A1 I'm not able to seperate these.. Does anyone have a hot tip? (It's hard to find anything on excel-automation on google..)
private bool IsWorkbookEmpty(Workbook excelBook)
{
try
{
if (excelBook.Sheets.Count <= 0)
{
return true;
}
else
{
foreach (Worksheet sheet in excelBook.Sheets)
{
Range excelRange = sheet.UsedRange;
int test1 = excelRange.Columns.Count;
int test2 = excelRange.Rows.Count;
int test3 = excelRange.Count;if (test1 > 1 || test2 > 1 || test3 > 1) { return false; } else //look for content.. { foreach(Range cell in excelRange) { object test5 = cell.FormulaR1C1; object test6 = cell.get\_Value(Type.Missing); //How do I check if cell has content? //return true; if cell has content.. } } } } } catch (Exception) { return false; } return true;
}
-
I'm trying to write a method to find out if a workbook is empty or not.. I'm using Excel 2007 and Microsoft.Office.Interop.Excel.Workbook. If the workbook has content in more than one cell I'm able to detect this, but if I have an empty "workbookA" and a "workbookB" with content in cell A1 I'm not able to seperate these.. Does anyone have a hot tip? (It's hard to find anything on excel-automation on google..)
private bool IsWorkbookEmpty(Workbook excelBook)
{
try
{
if (excelBook.Sheets.Count <= 0)
{
return true;
}
else
{
foreach (Worksheet sheet in excelBook.Sheets)
{
Range excelRange = sheet.UsedRange;
int test1 = excelRange.Columns.Count;
int test2 = excelRange.Rows.Count;
int test3 = excelRange.Count;if (test1 > 1 || test2 > 1 || test3 > 1) { return false; } else //look for content.. { foreach(Range cell in excelRange) { object test5 = cell.FormulaR1C1; object test6 = cell.get\_Value(Type.Missing); //How do I check if cell has content? //return true; if cell has content.. } } } } } catch (Exception) { return false; } return true;
}
Figured it out :thumbsup:
private bool IsWorkbookEmpty(Workbook excelBook) { try { if (excelBook.Sheets.Count <= 0) { return true; } else { foreach (Worksheet sheet in excelBook.Sheets) { Range excelRange = sheet.UsedRange; int test1 = excelRange.Columns.Count; int test2 = excelRange.Rows.Count; int test3 = excelRange.Count; if (test1 > 1 || test2 > 1 || test3 > 1) { return false; } else //look for content.. { foreach(Range cell in excelRange) { if (cell.Value2 != null) { string cellValue = cell.Value2.ToString(); if (cellValue.Trim().Length > 0) return false; } } } } } } catch (Exception) { return false; } retur
-
Figured it out :thumbsup:
private bool IsWorkbookEmpty(Workbook excelBook) { try { if (excelBook.Sheets.Count <= 0) { return true; } else { foreach (Worksheet sheet in excelBook.Sheets) { Range excelRange = sheet.UsedRange; int test1 = excelRange.Columns.Count; int test2 = excelRange.Rows.Count; int test3 = excelRange.Count; if (test1 > 1 || test2 > 1 || test3 > 1) { return false; } else //look for content.. { foreach(Range cell in excelRange) { if (cell.Value2 != null) { string cellValue = cell.Value2.ToString(); if (cellValue.Trim().Length > 0) return false; } } } } } } catch (Exception) { return false; } retur
WorkSheets = all your Worksheets private void deleteUnUsedWorksheets() { foreach (Excel._Worksheet sheet in WorkSheets) { if (IsWorkbookEmpty(sheet)) sheet.Delete(); } } private bool IsWorkbookEmpty(Excel._Worksheet sheet) { ExcelRange = sheet.UsedRange; if (ExcelRange.Columns.Count > 1 | ExcelRange.Rows.Count > 1 | ExcelRange.Count > 1) return false; else return true; } You dont have to check each cell. Its senseless and you throw a lot of memory Don`t throw a catch if its only a return false!