简体   繁体   English

如何从“ Series.Values”(Excel图表)访问值

[英]How can I access the values from `Series.Values` (Excel Chart)

I am using the Microsoft.Office.Interop.Excel namespace and I am creating a chart. 我正在使用Microsoft.Office.Interop.Excel命名空间,并且正在创建图表。 At a certain moment, I want to retrieve the values of a certain series. 在某个时刻,我想检索某个系列的值。 On MSDN it says that a Series object has a property Values . MSDN上,它说Series对象具有属性Values This returns either a Range object or an array of values , I assume an object[] . 这将返回Range对象或array of values ,我假设是object[] In my code I have the following statement: 在我的代码中,我有以下声明:

Series series = (Series)chart.SeriesCollection(i);
object[] values = (object[])series.Values;

I get an InvalidCastException with the message: Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'. 我收到一条InvalidCastException消息: Unable to cast object of type 'System.Object[*]' to type 'System.Object[]'.

When I debug, using Visual Studio 2008, I can inspect the type of series.Values and it says object{object[1..7]} . 当我使用Visual Studio 2008进行调试时,可以检查series.Values的类型,并显示为object{object[1..7]} This means (as I understand) that it is declared as an object but its actual type is object[1..7] . (据我所知)这意味着将其声明为object但其实际类型为object[1..7] But object[1..7] is not really a type I can cast to and neither is object[*] . 但是object[1..7]并不是我可以转换为的类型, object[*]也不是。

I suspect (or guess) that it might have something to do with the fact that the array starts at 1 instead of 0 (probably because of VB). 我怀疑(或猜测)它可能与数组从1而不是0开始的事实有关(可能是由于VB)。 I didn't even know you could define a 1 based array in c#... 我什至不知道您可以在C#中定义一个基于1的数组...

Even tough it might seem weird in C# to create non-zero index based arrays it is actually possible: 即使很难,但在C#中创建基于非零索引的数组似乎很奇怪,实际上是可能的:

var array = Array.CreateInstance(
    typeof(object), 
    new int[] { 7 }, 
    new int[] { 1 });

In your case I think you should be able to cast to an array and enumerate: 在您的情况下,我认为您应该能够转换为数组并枚举:

foreach (object item in (Array)series.Values)
{
}

And there's an interesting article explaining the saga around this types of arrays in the CLR. 有一篇有趣的文章解释了CLR中关于此类数组的传奇。

Actually why even define the array before you create it. 实际上,为什么还要在创建数组之前就定义它。 since you are getting the result back from .Values property. 因为您是从.Values属性返回结果。

This should work... 这应该工作...

Series series = (Series)chart.SeriesCollection[i]; 
object[,] values = (object[,])series.Values; 

Darin is correct it is 1 based, but it is also not a normal 2 dimensional array. Darin是正确的,它是基于1的,但它也不是普通的二维数组。 Range and other Excel objects use a 1 based "jagged" (i think this is the correct description) array since any range object is not necessarily contigiously aligned. 范围和其他Excel对象使用基于1的“锯齿状”(我认为这是正确的描述)数组,因为任何范围对象都不必连续对齐。

Using Array class as in Darin's suggestion will work since it accepts any form of object array. 如Darin建议那样使用Array类,因为它可以接受任何形式的对象数组,因此可以使用。 Its probably easier to work with using his example if you just need to enumerate the items. 如果您只需要枚举项目,使用他的示例可能更容易工作。

This is the problem between the Windows application vs. the VSTO model. 这是Windows应用程序与VSTO模型之间的问题。

foreach (object item in series.Values as Array)

Try this in case of VSTO. 如果是VSTO,请尝试此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM