使用 openXML 将图像添加到 excel

Adding an image to excel using openXML

我一直在尝试使用 openXML 将图像添加到工作表中的单元格中。我在搜索时发现的大多数解决方案使用的代码来自或类似于 PolymathProgrammer 网站上的代码:

http://polymathprogrammer.com/2009/11/30/how-to-insert-an-image-in-excel-open-xml/

当我尝试此操作时,我收到有关 NonVisualDrawingProperties 的错误,如下所示:

尽管我的用法应该包括它:

我做错了什么?或者我可以使用一些更简单的代码吗?

这是因为在不同的命名空间中有多个NonVisualDrawingProperties,而您在这里想要使用哪个是不明确的。

您可以通过完全限定名称来解决此问题:

var nvdp = new DocumentFormat.OpenXml.Drawing.Spreadsheet.NonVisualDrawingProperties();

您还可以 alias the using statement 避免输入太多内容:

using SPD = DocumentFormat.OpenXml.Drawing.Spreadsheet;
...
var nvdp = new SPD.NonVisualDrawingProperties();