Worksheet get_Range throws exception

后端 未结 4 1844
一整个雨季
一整个雨季 2020-12-13 19:09

I\'m using C# to manipulate an Excel worksheet. The following two pieces of code should work the same, but one works and the other throws an exception. I wonder why.

4条回答
  •  被撕碎了的回忆
    2020-12-13 19:18

    Use the Worksheet Range property instead. For example, instead of

    oRange = (Excel.Range)oSheet.get_Range(oSheet.Cells[1, 1],oSheet.Cells[4,4]);
    

    use

    oRange = (Excel.Range)oSheet.Range[oSheet.Cells[1, 1],oSheet.Cells[4,4]];
    

    I was using the get_Range() method extensively when I leveraged off .NET 2. When I changed to .NET 4 Client Profile, I got this exception also. Replacing the get_Range() references with the Range property addressed this issue for me.

提交回复
热议问题