如何用图片插入评论到Excel中使用C#

正如我在标题中所描述的,如何使用C#将图片作为注释插入到Excel中? 请提供一个示例代码或一些参考文件。 以下是我的代码:

using Excel=MicroSoft.Office.Interop.Excel; publice void ExcelEdit(string Path) { Excel.Application xlApp; Excel.WorkBook xlWorkBook; Excel.WorkSheet xlWorkSheet; Excel.Range myRange; xlApp=new Excel.ApplicationClass(); xlWorkBook=xlApp.WorkBooks.Open(Path, misValue, misValue,misValue, misValue,misValue, misValue,misValue, misValue,misValue, misValue,misValue, misValue,misValue, misValue) xlApp.Visable=True; xlWorkSheet=(Excel.WorkSheet)xlWorkBook.Sheets.get_Item(1); myRange=WorkSheet.Range[WorkSheet.Cells[1,1],WorkSheet.Cells[1,1]); xlWorkSheet.Cells[1,1]=InstertPictureComment(myRange,Path); myRange=WorkSheet.Range[WorkSheet.Cells[1,2],WorkSheet.Cells[1,2]); xlWorkSheet.Cells[1,1]=InstertPictureComment(myRange, Path); } public void InstertPictureComment(Excel.Range myrange, string picturepath) { myrange.ClearComment(); myrange.AddComment(); myrange.Comment.Shape.Fill.UserPicture(picturepath); myrange.Comment.Shape.Width=400; myrange.Comment.Shapes.Height=300; } 

我可以成功地将图片注释插入到Excel中。 问题是:当我复制并粘贴刚插入注释的单元格时,保存excel并closures它。 下次当我打开excel时,messagebox显示“找不到xxx中的不可读内容”。

如何处理我的代码!

这个问题似乎是重复插入图片评论与C#好的,而复制与评论内容失败

我刚刚在那里贴了一个答案。 在这里复制相同的答案。

我已经纠正了代码,以便编译

 public void InstertPictureComment(Excel.Range myrange, string picturepath) { myrange.Cells.ClearComments(); myrange.AddComment(); myrange.Comment.Shape.Fill.UserPicture(picturepath); myrange.Comment.Shape.Width = 400; myrange.Comment.Shape.Height = 300; } 

部分问题是使用Excel。 使用您的代码,您可能正在创build一个新的Excel应用程序实例。 Excel无法跨应用程序实例复制对象。

如果您在同一个应用程序实例中打开另一个工作簿,则这些对象将被复制。 在应用程序实例中复制数据的唯一方法是使用“粘贴特殊function”。

您应该获取现有的Excel应用程序实例。 如果不在那里,那么你可以创build它。

 private Excel.Application GetExcelInstance() { Excel.Application instance = null; try { instance = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); } catch (System.Runtime.InteropServices.COMException ex) { instance = new Excel.Application(); appCreatedExcelInstance = true; } return instance; } 

您可以使用appCreatedExcelInstance标志决定是否在清理过程中退出实例。

我希望这有帮助。