c#添加一个图片从资源excel

我目前正在做的下面添加一个图像到我创build的Excel文件通过“互操作”

private Excel.Workbook _xlWorkBook; _xlWorkSheet.Shapes.AddPicture(appPath + @"\ImageFile.png", Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, 5, 5, 60, 60); 

如果可以的话,我有几个问题。

  1. 添加图片后如何访问图片 – 例如在图片上绘制边框。
  2. 为我的应用程序做以上的手段,我必须分发的图像文件,所以我想我会把它放在应用程序资源。 如何将资源中的图像添加到excel文件中? 再一次添加,我如何访问它来添加边框等?

    _xlWorkSheet.Shapes.AddPicture(Properties.Resources.ImageFile); //不要工作

非常感谢

1)我相信你可以通过使用访问图片

 // after adding the picture Picture pic = (Picture) ActiveSheet.Pictures(ActiveSheet.Pictures.Count - 1); pic.Border.LineStyle = XlLineStyle.xlContinuous; pic.Border.Weight = XlBorderWeight.xlMedium; 

要么

 // add the picture using Pictures.Insert // this should return a Picture cast-able object Picture pic = (Picture) ActiveSheet.Pictures.Insert(FileName); // etc... 

2.)最简单的方法是从资源拉文件写入临时文件,将其添加到Excel中,然后删除临时文件。

这个代码是非常未经testing的。 Excel互操作是一个头痛。

对不起还是考古学。

对于第二个问题,从资源添加图片,我可能已经find了使用剪贴板的解决scheme:

 System.Drawing.Bitmap pic = Properties.Resources.my_ressource; System.Windows.Forms.Clipboard.SetImage(pic); Range position = (Range)myWorksheet.Cells[Y, X]; myWorksheet.Paste(position); //copy the clipboard to the given position