I am attempting to write a VSTO Add-in for Excel 2007 in C# and I am having problems reading values from previously generated chart开发者_如何学Cs. The Add-in duplicates a portion of a large data set and then replots the data with the same properties but different worksheet and sourcedata. When I attempt to obtain the chart title (and other properties) from the original worksheet, it causes an exception. I have attempted to use several variations of Globals.ThisAddIn.Application.Charts as a starting point but I have been unsuccessful. My assumption is that I am not referring to the old chart properly.
any help would be appreciated.
Thanks.
After some digging, I realized I should have been reading properties from the SeriesCollection of the chart. Something similar to the following worked
snippet Excel.ChartObjects xlCharts = (Excel.ChartObjects)newWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(10, 60, 720, 432);
Excel.Chart chart = myChart.Chart;
Excel.ChartObject oldchartObject = (Excel.ChartObject)VstoWorksheet.ChartObjects(1);
Excel.Chart oldchart = oldchartObject.Chart;
Excel.SeriesCollection seriesCollection = (Excel.SeriesCollection)chart.SeriesCollection(Type.Missing);
Excel.SeriesCollection oldSeriesCollection = (Excel.SeriesCollection)oldchart.SeriesCollection(Type.Missing);
Excel.Series oSeries;
for (int i = 1; i <= (ColumnCount - 2); i++)
{
oSeries = seriesCollection.NewSeries();
Excel.Series oOldSeries = oldSeriesCollection.Item(i);
oOldSeries = (Excel.Series)oldchart.SeriesCollection(i);
oSeries.MarkerStyle = oOldSeries.MarkerStyle;
oSeries.Name = oOldSeries.Name;
}
精彩评论