gpt4 book ai didi

.net - 使用剪贴板时出现 Spreadsheetgear 错误

转载 作者:行者123 更新时间:2023-12-04 19:49:31 25 4
gpt4 key购买 nike

在我的应用程序中,我使用电子表格工具来处理 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 文件切换两行。

最佳答案

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

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

  • 复制显然不会影响源范围; 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();

关于.net - 使用剪贴板时出现 Spreadsheetgear 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8170862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com