简体   繁体   English

如何从C#中的Excel.Worksheet中删除图像

[英]How to delete images from Excel.Worksheet in C#

I'm working on a C# document level customization VSTO for Excel. 我正在为Excel开发C#文档级自定义VSTO。 In it, I will come across some worksheets that have some Bitmaps put in them. 在其中,我将看到一些工作表,其中包含一些Bitmaps

What is the best way to clear them? 清除它们的最佳方法是什么?

In VBA the answer would be as simple as taking a Worksheet object then calling the method WorksheetX.Pictures.Clear() . 在VBA中,答案很简单,只要获取一个Worksheet对象,然后调用方法WorksheetX.Pictures.Clear() I tried a sort of direct C# translation of that: 我尝试了一种直接的C#翻译:

Excel.Worksheet TempSheet = Globals.ThisWorkbook.Worksheets(IndexVar);
TempSheet.Pictures.Clear();

But unfortunately Worksheet.Pictures has no available methods or properties. 但是很遗憾, Worksheet.Pictures没有可用的方法或属性。 I next thought I could find them in Worksheet.OLEObjects , but even going through that collection 1 by 1, I found that the images where not stored there. 接下来,我想到可以在Worksheet.OLEObjects找到它们,但是即使通过1到1进行收集,我也发现图像没有存储在那里。

I then tried to take a closer look at the MS list of Excel.Worksheet methods (and properties), and the only thing I could find that was close was Excel.Worksheet.Pictures , which contains the lovely note: 然后,我尝试仔细看一下MS.Excel.Worksheet方法 (和属性)的列表,我发现唯一关闭的是Excel.Worksheet.Pictures ,其中包含可爱的注释:

This API supports the Visual Studio infrastructure and is not intended to be used directly from your code. 此API支持Visual Studio基础结构,不能直接在您的代码中使用。

So now I'm back at my original question, how do I get the images contained within a worksheet? 因此,现在我回到最初的问题,如何获取工作表中包含的图像? Is there something obvious I'm missing? 有什么明显的我想念的东西吗?

My current workaround is to can a VBA micro, but that is exceedingly cumbersome. 我当前的解决方法是可以使用VBA micro,但这非常麻烦。 I'd rather handle it all in C# to avoid any VBA if possible. 我宁愿用C#处理所有内容,以尽可能避免使用任何VBA。

If I have more than 1 picture in the sheet, I am able to run: 如果工作表中有1张以上的图片,则可以运行:

Excel.Worksheet TempSheet = Globals.ThisWorkbook.Worksheets(IndexVar);
foreach (void XXX in TempSheet.Pictures) {
    if (XXX.Index > 1) {
        XXX.Delete();
    }
}

And that will leave 1 image behind (I guess it's 1 based not 0 based counting) 这将留下1张图片(我想这是基于1而不是基于0的计数)

But if I just try: 但是,如果我只是尝试:

Excel.Worksheet TempSheet = Globals.ThisWorkbook.Worksheets(IndexVar);
foreach (void XXX in TempSheet.Pictures) {

        XXX.Delete();

}

Then it causes Excel to crash. 然后,它导致Excel崩溃。

I'm not an user of VSTO, but it seems that you are left with one picture as you are deleting all pictures with index above 1. I suggest to change this line: 我不是VSTO的用户,但是在删除索引大于1的所有图片时,似乎只剩下一张图片。建议更改此行:

if (XXX.Index > 1) {

to: 至:

if (XXX.Index >= 1) {

Hopefully it will delete all now. 希望它现在将全部删除。

First identify the picture count and delete. 首先确定图片数量并删除。

The below line of code will help you to identify the count of pictures in the workseet. 下面的代码行将帮助您识别工作台中的图片数量。

WorkSheet.Pictures().Count WorkSheet.Pictures()。Count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM