简体   繁体   中英

updating Excel Series Chart in C#

I'm in the process of recreating a VBA program in C# and have run into an issue while updating Series charts. I've spent an amazing amount of time on google and forums looking to find the answer to my issue for myself, but cannot seem to figure it out.

What I'm looking to do is update a series chart to look at data from a worksheet that is not where the chart is located, and update the range.

Where my issue comes in is that when updating the series chart, C# will only accept a range. However, the range that the Series chart requires contains a sheet name, and a range cannot contain text (So I'm led to believe). an example of the range I need to recreate is as follows;

=ANZ!$J$313:$J$737,ANZ!$M$313:$O$737

The code I currently have is:

CloudAllTrialsDataPage = UpdateBook.Worksheets[CloudAllTrialsChartDataPage1];
LastRow = CloudAllTrialsDataPage.Range["J:J"].Find(StartDate, Type.Missing, XlFindLookIn.xlValues, XlLookAt.xlWhole, XlSearchOrder.xlByRows, XlSearchDirection.xlNext, Type.Missing, Type.Missing, Type.Missing).Row;
FoundCell = CloudAllTrialsDataPage.Range["J:J"].Find(strDateLM, Type.Missing, XlFindLookIn.xlValues, XlLookAt.xlWhole, XlSearchOrder.xlByRows, XlSearchDirection.xlNext, Type.Missing, Type.Missing, Type.Missing).Row;
Excel.Range Range1 = CloudAllTrialsDataPage.Range["J" + FoundCell + ":J" + LastRow];
Excel.Range Range2 = CloudAllTrialsDataPage.Range["M" + FoundCell + ":O" + LastRow];
Range DataRange = CloudAllTrialsDataPage.Range[Range1].Address + CloudAllTrialsDataPage.Range[Range2].Address;
chartObject2 = CloudAllTrialsChartPage.ChartObjects(Type.Missing);
myChart = (Excel.ChartObject)chartObject2.Item("Monthly2");
myChart.Chart.SetSourceData(DataRange);

All of the code pulls correctly and works right up until the point of inserting the range into the chart. I've tried various different workarounds but always run into the issue of trying to add the sheet name to the range.

Any assistance or pointers would be most gratefully appreciated.

Thank you.

Disclaimer: I'm a layman so excuse me if any of my terminology is "incorrect".

OK. So i figured out the two issues i was having with this.

1) It often helps if you're referencing the right chart.

2) To combine the two separate ranges into the series chart range, use a get_Range. If you set the variable as,

Excel.Range ChartRange = datapage.get_Range("A1:A6","C1:C6");

this creates a range containing two ranges. As the declared range is not on the same sheet as the chart, the sheet name is automatically filled in.

The final code i settled on was;

Excel.Range chartRange;
Excel.ChartObjects xlCharts = (Excel.ChartObjects)CloudAllTrialsChartPage.ChartObjects(Type.Missing);
Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Item("DailyView");
Excel.Chart chartPage = myChart.Chart;
chartRange = CloudAllTrialsDataPage.get_Range("J" + FoundCell + ":J" + LastRow + ",M" + FoundCell + ":O" + LastRow);
chartPage.SetSourceData(chartRange, Missing.Value);

Just going to leave this here in case anyone else needs it. There is surprisingly little advice on updating series charts available. (That i could find.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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