“object”未包含“get_Range”的定义

在VS2010中调用COM Interop DLL操作Excel通过get_Range去获取Range时,会发生Object does not contain a definition for get_Range的错误。其原因和解决方案:

Misha's explanation is correct - when using No PIA, methods returning object are treated as if they return dynamic in order to simulate the VBA semantics of COM Variants. Because the return value of sh.Cells is Object, sh.get_Range is dispatched dynamically, and the dynamic COM binder does not support the get_Range syntax exposed in C# before indexed properties were supported. We've tried to maintain backwards compatibility wherever possible when you turn on Embed Interop Types for a COM reference, but this is one place where some further tweaking is required.


The workaround you proposed will work - a cleaner workaround is the one Mike Rosenblum pointed out to use C# 4.0's new indexed properties syntax, which the dynamic COM binder does understand. You can then represent the operation with the following code:

Excel.Range r = sh.Range[sh.Cells[1, 1], sh.Cells[2, 2]];

See Also

get_Range method missing with embedded interop assembly

该问题的解决办法是:将下面的行1修改成行2的形式,这样就解决了。

             行1: workSheet.get_Range(workSheet.Cells[2,1],workSheet.Cells[rowCount+1,colCount]).Value2=data;

             行2:range = workSheet.Range[workSheet.Cells[2, 1], workSheet.Cells[rowCount + 1, colCount]];

            range.Value = data;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值