MS Excel com问题 - 使用Excel com对象时无法从剪贴板中取出图像

发布于 2025-01-22 08:48:24 字数 1405 浏览 3 评论 0 原文

在使用Excel Com Object范围时,无法从剪贴板中取出图像。

我本地计算机上的电视观方法一切正常,但是在服务器上,我面临此问题。 本地机器我安装了2016年Office,我们在服务器上安装了Excel-2013。

部署后,当我运行Web应用程序时,它将抛出异常(对象ref不设置实例)。 但是,当我直接在任何Word Doc中手动执行CTRL+V(粘贴)时,然后将图像粘贴到那里时,但是从代码中粘贴了图像。

代码 -

     System.Threading.Thread.Sleep(2000);
     xlApp.CutCopyMode = XlCutCopyMode.xlCopy;
     Range range = xlWorkSheet.get_Range(startRange, endRange);                                              
     System.Windows.Forms.Clipboard.Clear();               
                            
     range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
     
     //Here getting null in imgWS1                                     
     Image imgWS1 = System.Windows.Forms.Clipboard.GetImage(); 
                              
     string fileName = Path.Combine(reportPath, imageName);

     //Getting exception- object ref not to set an instance..           
     imgWS1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 

我尝试过的东西 1.)当我在剪贴板中添加测试图像时

Forms.Clipboard.SetImage(imgtest);

Clipboard.GetImage() 

正常工作。因此,在这里,我认为复制方法有一些问题。

2.)我还尝试使用DataObject,但没有运气。

 System.Windows.Forms.DataObject o = Forms.Clipboard.GetDataObject() as DataObject;
 System.Windows.Forms.Clipboard.SetImage(GetClipboardImage(o));

Unable to get the image out from Clipboard while using excel COM object Range.CopyPicture method

On my local machine everything is working fine but on server I am facing this issue.
Local machine I have office 2016 installed and at server we have Excel-2013 installed.

After deployment when i run the web application then it is throwing exception (object ref not to set instance).
But when I go to server and manually doing ctrl+v (paste) in any word doc directly then it pasting the image there, But from the code it is not able to read the image.

Code-

     System.Threading.Thread.Sleep(2000);
     xlApp.CutCopyMode = XlCutCopyMode.xlCopy;
     Range range = xlWorkSheet.get_Range(startRange, endRange);                                              
     System.Windows.Forms.Clipboard.Clear();               
                            
     range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
     
     //Here getting null in imgWS1                                     
     Image imgWS1 = System.Windows.Forms.Clipboard.GetImage(); 
                              
     string fileName = Path.Combine(reportPath, imageName);

     //Getting exception- object ref not to set an instance..           
     imgWS1.Save(fileName, System.Drawing.Imaging.ImageFormat.Jpeg); 

Things I have tried
1.) When I am adding the test image in clipboard through

Forms.Clipboard.SetImage(imgtest);

then

Clipboard.GetImage() 

is working fine. So here I think some problem is with the CopyPicture method.

2.) I also tried using DataObject but had no luck.

 System.Windows.Forms.DataObject o = Forms.Clipboard.GetDataObject() as DataObject;
 System.Windows.Forms.Clipboard.SetImage(GetClipboardImage(o));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文