【问题标题】:Translate Copy / Paste in excel from VBA code to C#将 Excel 中的复制/粘贴从 VBA 代码转换为 C#
【发布时间】:2020-09-20 11:56:25
【问题描述】:

我有这个 vba 代码,我正在尝试使用 Microsoft.Office.Interop.Excel 将其转换为 C#。

So the code is : 

     Columns("AN:AS").Select
                Selection.Copy
                Columns("AT:AT").Select
                Selection.Insert Shift:=xlToRight
                Columns("AT:AY").Select
                Selection.Replace What:="ST", Replacement:="TO", LookAt:=xlPart, _
                    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
                    ReplaceFormat:=False
                Application.CutCopyMode = False

我已经研究了一些解决方案,但我不断收到错误。

这就是我所做的:

      Range source = (Microsoft.Office.Interop.Excel.Range)currentSheet.get_Range("AN:AS").Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight);
                       Range destination =  currentSheet.get_Range("AT: AT");
                        source.Copy(destination);

      currentSheet.get_Range("AT:AY").Replace("ST", "TO", SearchOrder : 1 , LookAt : 2,  MatchCase: false, SearchFormat: false, ReplaceFormat: false);
                        currentSheet.Application.CutCopyMode = 0;

And i got error at the source variable saying : 

An exception of type 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' occurred in System.Core.dll but was not handled in user code

Additional information: Impossible de convertir le type 'bool' en 'Microsoft.Office.Interop.Excel.Range'

我的目标是将代码从 VBA 转换为 C#。

【问题讨论】:

  • get_Range( 应该是Range[
  • 也试过了。仍然遇到一些错误。我不知道我是否对 excel 格式(xls 与 xlsx)有问题?但我明确地尝试了 Range [] 并且仍然无法正常工作。
  • CutCopyMode 是一个布尔属性,但您在 c# 代码中分配了一个 int?
  • @KostasK。实际上,当我分配源时,错误就在第一行。谢谢
  • 尝试中断源分配。 source = currentSheet.get_Range("AN:AS");。然后source.Insert(XlInsertShiftDirection.xlShiftToRight);

标签: c# excel vba excel-interop


【解决方案1】:

我的目标是将代码从 VBA 转换为 C#。

应该做的工作:

        using Excel = Microsoft.Office.Interop.Excel;


        Excel.Range source = currentSheet.get_Range("AN:AS");
        Excel.Range destination = currentSheet.get_Range("AT:AT");

        destination.Insert(XlInsertShiftDirection.xlShiftToRight, source.Copy());

        currentSheet.get_Range("AT:AY").Replace("ST", "TO", SearchOrder: 1, LookAt: 2, MatchCase: false, SearchFormat: false, ReplaceFormat: false);

        // avoid to have a message about clipboard before saving the file 
        currentSheet.Application.CutCopyMode = XlCutCopyMode.xlCopy;

【讨论】:

    【解决方案2】:

    感谢大家的回复。

    这对我来说终于有用了。

          Range source = currentSheet.get_Range("AN:AS");
                    source.Select();
                    source.Copy();
    
                    Range destination = (Microsoft.Office.Interop.Excel.Range)currentSheet.get_Range("AT:AT");
    
                    destination.Insert(Microsoft.Office.Interop.Excel.XlInsertShiftDirection.xlShiftToRight);
    
    
                    Range range_sheet = currentSheet.get_Range("AT:AY");
                    range_sheet.Select();
                    range_sheet.Replace("ST", "TO", SearchOrder: XlSearchOrder.xlByRows, LookAt: XlLookAt.xlPart, MatchCase: false, SearchFormat: false, ReplaceFormat: false);
                    currentSheet.Application.CutCopyMode = 0;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多