How to get Excel XValue chart in C #

I am writing a few codes to manipulate a diagram in an excle file using C #. One of the key requirements is to obtain the XValue property of the SeriesCollection chart. I tried to visit it by index, but it does not work, although I see a list of values ​​in the Clock window in Visual Studio. From the Observation window, I noticed that the type s.XValue dynamic {object []} , which I had never seen before, and obj.ToString () is System.Object [*] . The question is, how can I get each value from the XValue property if XValue does NOT support the index.


 

By the way, searching the Internet, I can find ways to set the range of Excel cells to XValue, but have not found how to get XValue. I really appreciate if anyone can advise.

Excel._Worksheet worksheet = (Excel._Worksheet)book.Worksheets[a];
Excel.ChartObject chartObj = (Excel.ChartObject)worksheet.ChartObjects(1);
if (chartObj != null)
{
   Excel.Chart chart = (Excel.Chart)chartObj.Chart;
   var s = (Excel.Series)chart.SeriesCollection(1);
   object obj = s.XValues;
   //How to get s.XValues[1], s.XValues[2], s.XValues[3]...
}
+4
source share
1 answer

Try the following:

var chart = chartObj.Chart as Excel.Chart;
var s = chart.SeriesCollection(1) as Excel.Series;

var xValues = (s.XValues as object) as Array;

foreach (var xVal in xValues)
{
    MessageBox.Show(xVal.ToString());
}

and also What is this type of `* [*]` that I get through COM interoperability?

+3
source

All Articles