可存储在 Excel 中的值
晚上好!
哪些类型的值可以使用 Range.Value2
直接存储到 Excel 工作表中?如何快速检查特定值是否可以?
假设我有一个对象数组,可能是多类型的(例如,一个 int
、一个 double
和一个 Foo
存储在 object[] 中)。
如果我选择宽度为 3 的范围并尝试使用 Range.Value2
存储该数组,这将导致异常(当然 Excel 不知道什么是 Foo
代码>)。
我想到了检查数组中的每个值的想法,如果它不可存储,则使用 ToString() 将其转换为字符串表示形式。但我如何检查它是否最初可存储?
最终做这样的事情将是可怕的:
public bool storable<T>(T value)
{
return value is int ||
value is uint ||
value is short ||
value is byte ||
...
value is string;
}
...特别是知道每个 is
都会将变量强制转换到测试的类型并严重影响性能。
另一方面,我无法将 each 值预先转换为 string
类型,因为我有时希望能够使用数值来绘制图形和图表,而不是字符串。
你能告诉我我错了或者为我提供解决问题的方法吗? 谢谢你!
Good evening!
Which types of values can be directly stored into an Excel worksheet using Range.Value2
and how do I quickly check if a particular value can?
Suppose I have an array of objects, perhaps multityped (e.g. one int
, one double
and one Foo
stored in an object[]
).
If I shall choose a range of width 3 and try to store this array using Range.Value2
, this will result in an exception (of course Excel doesn't know what is a Foo
).
I came up with an idea of checking each value in the array, and, if it's not storable, convert it to its string representation using ToString()
. But how do I check if it's initially storable?
It would be horrible to end up doing something like that:
public bool storable<T>(T value)
{
return value is int ||
value is uint ||
value is short ||
value is byte ||
...
value is string;
}
...especially knowing that each is
will cast the variable to the tested type and seriously affect performance.
On the other hand, I can't afford pre-casting each value to the string
type as I sometimes want to be able to do graphs and diagrams with numeric values, not strings.
Can you tell me I am mistaken or offer me any solution to the problem?
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你将不得不做你不愿意做的事情(所有“是”检查),除非你能以某种方式使你的输入数组具有更强的类型。您最好的选择可能只是对演员进行排序,以便最常见的演员首先受到打击。
I think you're going to have to do what you're unkeen to do (all the "is" checks), unless you can somehow make your input array a bit more strongly typed. Your best bet might be just to order the casts such that the most common ones get hit first.