How to add multiple rows to a chart in Excel using C #

I would like to add a diagram as shown in the following figure.

Excel chart

This chart has 3 series (black, red, blue).

Below is a block of code that creates a โ€œoneโ€ series in a diagram ...

Excel._Workbook oWorkbook = (Excel._Workbook)oSheet.Parent; Excel._Chart oChart = (Excel._Chart)oWorkbook.Charts.Add(oSheet, Type.Missing, Type.Missing, Type.Missing); // Y axis data Excel.Range oRange = oSheet.get_Range(yRange, Type.Missing); // Creates a chart oChart.ChartWizard(oRange, chartType, 2, Excel.XlRowCol.xlColumns, Type.Missing, Type.Missing, false, title, xAxisTitle, yAxisTitle, Type.Missing); // Sets X axis category Excel.Series oSeries = (Excel.Series)oChart.SeriesCollection(1); oSeries.XValues = oSheet.get_Range(xRange, Type.Missing); oChart.Name = chartName; 

The MSDN API is not useful enough, and I can hardly find any tutorial or example on this issue. (Or maybe I do not understand them so well)
It would be nice if someone gave me a solution.

+4
source share
2 answers

I could solve this problem with a very simple solution.
If I installed yRange (oRange) correctly, the "ChartWizard" method automatically creates charts.
Therefore, instead of a range having "A2: A100", "A2: A100, C2: C100" generates two rows (series) in one diagram, and also, if the data range includes a heading (or a serial label), "ChartWizard" will automatically place The name of the series in the legend.

+3
source

I am not too familiar with C #, but you can try something line by line

 Excel.Range NewRangeObject = oSheet.get_Range(SecondyRange, Type.Missing); oChart.NewSeries oChart.SeriesCollection(2).Name = "New Series" oChart.SeriesCollection(2).Value = NewRangeObject 

To be clear, I guess what the VBA code will look like.

A higher level solution is to write the macro in excel directly from the chart setup exactly the way you want .... then move the code to C #. Most of the commands seem to be similar, but wrapped in a slightly different snytax.

0
source

Source: https://habr.com/ru/post/1381868/


All Articles