Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在excel中复制选定范围时,如何避免此异常?_C#_Excel - Fatal编程技术网

C# 在excel中复制选定范围时,如何避免此异常?

C# 在excel中复制选定范围时,如何避免此异常?,c#,excel,C#,Excel,我又问了一个问题。答案背后的逻辑是正确的。但是我在调用CopyPicture(System.Runtime.InteropServices.COMException)时遇到了一个异常 var a=new Microsoft.Office.Interop.Excel.Application(); 工作簿w=a.Workbooks.Open(@“C:\scratch\blueyellow.xlsx”); 工作表ws=w.Sheets[“StatusR”]; ws.Protect(内容:false);

我又问了一个问题。答案背后的逻辑是正确的。但是我在调用CopyPicture(System.Runtime.InteropServices.COMException)时遇到了一个异常

var a=new Microsoft.Office.Interop.Excel.Application();
工作簿w=a.Workbooks.Open(@“C:\scratch\blueyellow.xlsx”);
工作表ws=w.Sheets[“StatusR”];
ws.Protect(内容:false);
线程。睡眠(3000);//修正(有时)
范围r=ws.范围[“B4:P24”];

r、 CopyPicture(xlPictureApearance.xlScreen,XlCopyPictureFormat.xlBitmap);// 经过失败的研究,我最终得到了一个不雅观的解决方案:

var errorCounter = 0;
var copyDone = false;
do {
   try {
      r.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
      copyDone = true;
   } catch {
      ++errorCounter;
} while (!copyDone && errorCounter <= 100);

if (errorCounter == 100) throw new ApplicationException("Unable to copy the selected range.");
var errorCounter=0;
var copyDone=false;
做{
试一试{
r、 CopyPicture(xlPictureApearance.xlScreen、XlCopyPictureFormat.xlBitmap);
copyDone=true;
}抓住{
++误差计数器;

}while(!copyDone&&errorCounter Idk,如果有帮助的话,但是您在搜索过程中找到了吗?谢谢您的链接。遗憾的是,在编写自己的解决方案之前,我尝试了这篇文章所说的内容,以及在互联网上找到的所有内容(实际上很少)。
var errorCounter = 0;
var copyDone = false;
do {
   try {
      r.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
      copyDone = true;
   } catch {
      ++errorCounter;
} while (!copyDone && errorCounter <= 100);

if (errorCounter == 100) throw new ApplicationException("Unable to copy the selected range.");