C#:Excel:确定字符串是否为有效的Range引用? [英] C# : Excel : Determine if a string is a valid Range reference?
问题描述
这个VB函数看起来像C#一样吗?目的是确定字符串ref
是否表示有效的数据表Excel.Range
.这不是普通的VB到C#的转换,因为不能构造Range.
How would this VB function will look like as C# ? The goal is to determine if a string ref
represents a valid datasheet Excel.Range
. This isn't a trivial VB to C# conversion as Range can't be constructed.
Private Function IsRange(ref) As Boolean
' Returns True if ref is a Range
Dim x As Range
On Error Resume Next
Set x = Range(ref)
If Err = 0 Then IsRange = True Else IsRange = False
End Function
最佳
推荐答案
我想找到一种不必依赖异常来检查单元格引用有效性的方法.但是我找不到直接测试的方法.
I would have liked to find a way to not have to rely on an exception for checking the validity of a cell reference. But I couldn't not find a way to directly test for it.
因此,我使用的是OP的方法.问题在于Microsoft.Office.Interop.Excel.Range
是一个接口,不能直接创建.对此 answer ,解决方案是使用_Application
或Sheet
中的get_Range
方法.
So instead I am using the OP's approach. The problem is that Microsoft.Office.Interop.Excel.Range
is an interface and can't be directly created. Per this answer the solution is to use the get_Range
method from _Application
or Sheet
.
Microsoft.Office.Interop.Excel._Application ExcelApp;
private bool IsRange(string refr) {
try {
var unused = ExcelApp.Range[cellReference, Type.Missing];
return true;
}
catch {
return false;
}
}
这篇关于C#:Excel:确定字符串是否为有效的Range引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!