C# 插入带有C Ok的图片注释,而复制带有注释的内容失败
我正在使用C编写excel,但在将图片注释插入excel时遇到了一些问题。 这是我的密码:C# 插入带有C Ok的图片注释,而复制带有注释的内容失败,c#,excel,comments,C#,Excel,Comments,我正在使用C编写excel,但在将图片注释插入excel时遇到了一些问题。 这是我的密码: public void InstertPictureComment(Excel.Range myrange, string picturepath) { myrange.ClearComment(); myrange.AddComment(); myrange.Comment.Shape.Fill.UserPicture(picturepath); myrange.Comm
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并关闭excel应用程序
==>重新打开excel,一个消息框告诉excel在xxxxx中发现无法读取的内容
我的代码怎么了?或者是excel问题?我已经更正了代码,以便编译
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。使用您的代码,您可能正在创建一个新的Excel应用程序实例。Excel无法跨应用程序实例复制对象
如果在同一应用程序实例中打开另一个工作簿,将复制对象。跨应用程序实例复制数据的唯一方法是使用“粘贴特殊”功能
您应该获取现有的Excel应用程序实例。如果它不在那里,那么您可以创建它
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标志来决定是否在清理期间退出实例