【发布时间】: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