This project has moved and is read-only. For the latest updates, please go here.

Access only ExcelShapes

Apr 28, 2015 at 11:29 AM
Hi, I am trying to access checkboxes from my worksheet.
foreach ( OfficeOpenXml.Drawing.ExcelShape ch in xlWorkSheetinc.Drawings)
                                    {
                                        Console.WriteLine(ch.Name);
                                    }
unfortunately the code above returns ExcelPicture too, and gives an exception:
System.InvalidCastException: Unable to cast object of type 'OfficeOpenXml.Drawing.ExcelPicture' to type 'OfficeOpenXml.Drawing.ExcelShape'.
Apr 28, 2015 at 2:10 PM
This is working:
    foreach (OfficeOpenXml.Drawing.ExcelDrawing ch in xlWorkSheetinc.Drawings)
      {
        if (ch.Name.Contains("Check"))
        {
          OfficeOpenXml.Drawing.ExcelShape c = (OfficeOpenXml.Drawing.ExcelShape)ch;
        }
      }
This way I can access the ExcelShape's properties.
For my luck Excel has an incremental naming. So every Checkbox I create and not renaming it will look like Check Box 1, Check Box 2 and so on.
Marked as answer by nlevente on 4/28/2015 at 6:10 AM