将图片添加到Excel工作表

时间:2017-11-19 11:17:45

标签: c# excel winforms

我试图将图片添加到我的Excel工作表中。这是我的代码:

string AppDirectory = AppDomain.CurrentDomain.BaseDirectory;
Range oRange = (Range)ar.Cells[2, 1];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageSize = 32;
ar.Shapes.AddPicture(AppDirectory + "ReportIcon" + "\\Bedab-Logo-Blue", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, (float)1.41, (float)0.4);
                        oRange.RowHeight = ImageSize + 2;

但是这行代码,

ar.Shapes.AddPicture(AppDirectory + "ReportIcon" + "\\Bedab-Logo-Blue", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, (float)1.41, (float)0.4);

抛出以下异常:

  

未处理的类型异常   ' System.Runtime.InteropServices.COMException'发生在mscorlib.dll

     

其他信息:找不到指定的文件。

注意:路径正确

1 个答案:

答案 0 :(得分:1)

我认为你缺少文件扩展名尝试添加像.jpg .png等的Bedab-Logo-Blue扩展名

ar.Shapes.AddPicture(AppDirectory + "ReportIcon" + "\\Bedab-Logo-Blue.JPG",
Microsoft.Office.Core.MsoTriState.msoFalse, 
Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, (float)1.41, (float)0.4);

我添加了.JPG,你必须根据你想要的文件类型添加