c# - 将 Excel 中的复制/粘贴从 VBA 代码转换为 C#
问题描述
我有这个 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#。
解决方案
我的目标是将代码从 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;
推荐阅读
- c# - 在winforms中使用按钮移动水平滚动条或列表框
- database - postgresql 多租户模式与数据库
- apache-kafka - 无法消费或生产到远程 Kafka 代理
- npm - 电子安装错误:为“electron-v2.0.2-win32-x64.zip”生成的校验和与预期的校验和不匹配
- python - groupby 一列并计算另一个熊猫中超过 5 的项目
- mysql - 插入错误 #1452 - 无法添加或更新子行:外键约束失败
- java - 检测到新的指纹登记
- angular - 角材料日期选择器自定义格式
- anylogic - AnyLogic 如何将一个代理链接到另一个代理
- kubernetes - k8s DaemonSet:被拒绝的 Pod 重新创建太快而无法获取任何日志或事件