将excel中的复制/粘贴从VBA代码转换为C#
我有一个vba代码,我正试图使用Microsoft.Office.Interop.Excel将其转换为C将excel中的复制/粘贴从VBA代码转换为C#,c#,excel,vba,excel-interop,C#,Excel,Vba,Excel Interop,我有一个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:
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
我的目标是将该代码从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;
谢谢你的回复 这就是我最终的工作
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;
get\u Range(
应该是Range[
也尝试过。仍然出现一些错误。我不知道excel格式(xls vs xlsx)是否有问题?但我确实尝试了Range[]仍然不起作用。CutCopyMode
是一个布尔属性,但您在c#code?@KostasK中分配了一个int。不,事实上,当我分配源代码时,错误在第一行。感谢您中断源代码分配。source=currentSheet.get_Range(“an:AS”)
。然后source.Insert(XlInsertShiftDirection.xlShiftToRight);