使用剪贴板时出现 Spreadsheetgear 错误

发布于 2024-12-15 22:52:36 字数 816 浏览 0 评论 0原文

在我的应用程序中,我使用电子表格齿轮来处理 Excel 文件。我使用以下代码:

//move incorrect to aux                         
incorrectLine.Select(); //select the line to be moved
wbkView.Cut();

auxLine.Select();
wbkView.Paste();

//move correct to incorrect
correctLine.Select(); //select the line to be moved
wbkView.Cut();

incorrectLine.Select();
wbkView.Paste();

//move aux to correct 
auxLine.Select(); //select the line to be moved
wbkView.Cut();

correctLine.Select();
wbkView.Paste();

我不时收到以下错误:

Requested Clipboard operation did not succeed. 

使用 StrackTrace:

at System.Windows.Forms.Clipboard.ThrowIfFailed(Int32 hr)

ErrorCode = -2147221040

据我了解,如果剪贴板被其他进程使用,则会出现问题,所以我想知道是否存在另一种模式可以在不使用剪贴板的情况下从 Excel 文件切换两行。

In my application I use spreadsheetgear to work with Excel files. I use the following code:

//move incorrect to aux                         
incorrectLine.Select(); //select the line to be moved
wbkView.Cut();

auxLine.Select();
wbkView.Paste();

//move correct to incorrect
correctLine.Select(); //select the line to be moved
wbkView.Cut();

incorrectLine.Select();
wbkView.Paste();

//move aux to correct 
auxLine.Select(); //select the line to be moved
wbkView.Cut();

correctLine.Select();
wbkView.Paste();

I receive from time to time the following error:

Requested Clipboard operation did not succeed. 

with the StrackTrace:

at System.Windows.Forms.Clipboard.ThrowIfFailed(Int32 hr)

and ErrorCode = -2147221040

From what I understand there is a problem if the clipboard is used by other process so I want to know if exists another mode to switch two lines from an Excel file without using the clipboard.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时光的容颜 2024-12-22 22:52:36

您也许可以使用 IRange.Copy(...) 方法而不是 WorkbookView.Cut/Paste。此方法在 SpreadsheetGear 内部执行复制例程,无需使用 Windows 剪贴板,因此可以避免出现类似您在此处看到的问题。另外,您不需要所有这些 Select 方法调用来在工作表中导航。

明显的问题是,这是一个复制例程,而不是剪切例程,它们的行为在很多方面都不同:

  • 复制显然不影响源范围; cut 从源范围中删除值和格式。您可以通过在调用 IRange.Copy() 后通过 IRange.Clear() 清除源范围来解决此问题。
  • 更巧妙的是,如果您的源范围包含公式或命名范围,则在使用复制而不是剪切时,这些公式和命名范围内的引用可能会以不同的方式修复。对此我们无能为力,因为这就是 Excel 的工作方式,而 SpreadsheetGear 在这些方面也遵循 Excel 的做法。当然,如果您的源范围只是没有命名范围的简单值,那么这不是问题。

将它们放在一起,您的代码可能如下所示:

// move incorrect to aux
incorrectLine.Copy(auxLine);
incorrectLine.Clear();

// move correct to incorrect
correctLine.Copy(incorrectLine);
correctLine.Clear();

// move aux to correct
auxLine.Copy(correctLine);
auxLine.Clear();

You might be able to use the IRange.Copy(...) method instead of WorkbookView.Cut/Paste. This method performs a copy routine internally within SpreadsheetGear, without the use of the Windows Clipboard, so would avoid any problems like the one you are seeing here. Plus, you wouldn’t need all those Select method calls to navigate around the worksheet.

The obvious problem is that this is a copy routine and not a cut routine, which behave different from one another in a variety of ways:

  • Copy obviously leaves the source range untouched; cut removes the value and formatting from the source range. You could work around this by clearing out the source range via IRange.Clear() after calling IRange.Copy().
  • More subtly, if your source range contain formulas or named ranges, references within these formulas and also named ranges will potentially be fixed up in different manner when using copy instead of cut. Not much can be done about this as this is simply the way Excel works, and SpreadsheetGear follows Excel’s lead on these things. Of course, if your source range is just simple values with no named ranges, this isn’t a problem.

Putting it all together, your code might look like the following:

// move incorrect to aux
incorrectLine.Copy(auxLine);
incorrectLine.Clear();

// move correct to incorrect
correctLine.Copy(incorrectLine);
correctLine.Clear();

// move aux to correct
auxLine.Copy(correctLine);
auxLine.Clear();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文