Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to check if Excel-workbook is empty?

How to check if Excel-workbook is empty?

Scheduled Pinned Locked Moved C#
questioncomtestingtoolstutorial
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thomas ST
    wrote on last edited by
    #1

    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;
    

    }

    T 1 Reply Last reply
    0
    • T Thomas ST

      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;
      

      }

      T Offline
      T Offline
      Thomas ST
      wrote on last edited by
      #2

      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
      
      U 1 Reply Last reply
      0
      • T Thomas ST

        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
        
        U Offline
        U Offline
        User 7628281
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups