How to Remove Pictures from Excel Spreadsheet by row, column or cell in C#

How to remove pictures from excel spreadsheet by row, column or cell. I don’t understand why I should ask something that clearly should be easy to do. At least one way to detect the image that is in a specific row.

@Damian45435236,

Please note, in MS Excel file formats, images/pictures are stored separately. That’s why you cannot detect/extract these directly using its corresponding cell/row/column, etc. You may use Shape/Picture specific attributes (e.g. UpperLeftRow, UpperLeftColumn, LowerRightColumn, LowerRightRow, etc.) accordingly to accomplish the task.

Steps on how to remove pictures from Excel spreadsheet by row, column or cell in C#

  1. Add reference Aspose.Cells for .NET to project using NuGet repos.
  2. Add reference to Aspose.Cells namespace
  3. Instantiate the Workbook to load an existing Excel file
  4. Get PictureCollection from the first worksheet in the workbook
  5. Loop through from last picture till first picture in the collection
  6. Evaluate if the picture is pasted in your desired cell range, so you may remove it
  7. Save the Excel workbook

See the sample code that will guide you on how to remove pictures from Excel spreadsheet by row, column or cell in C# for your reference. So, you may write your own code accordingly for your needs:

Sample code on how to remove pictures from Excel spreadsheet by row, column or cell in C#

...
using System;
using Aspose.Cells;
...

string filePath = "e:\\test2\\Book1.xlsx";

Workbook workbook = new Workbook(filePath);
PictureCollection pictures = workbook.Worksheets[0].Pictures;

for (int i = pictures.Count - 1; i >= 0; i--)
{
                   
        //If the picture is contained (starting drawn from) in the F9 cell.
        if (pictures[i].UpperLeftColumn == 5 && pictures[i].UpperLeftRow == 8)
        {
              pictures.RemoveAt(i);
        }

                                    
}

workbook.Save("e:\\test2\\out1.xlsx");

Hope, this helps a bit.

1 Like

Thanks, I tried that, but I typed the wrong column (22), but with your help I understood that I was on the right track and it occurred to me to detect the column and row first (it was 21). It works perfect, thank you very much

                    //Test code, to confirm column and row
                        if(img.Name == "Picture 907"){
                         var c=  img.UpperLeftColumn;
                         var r= img.UpperLeftRow;
                        }

                       // Your code without my mistakes
                        if (img.UpperLeftColumn == 21 && img.UpperLeftRow == 73)
                        {
                            hoja.Pictures.RemoveAt(i);
                        }

Thanks again

@Damian45435236,
You are welcome.