C# 使用 Excel Interop 添加辅助轴

C# Adding secondary Axis using Excel Interop

我构建的应用程序将从 SQL 数据库创建 Excel 电子表格。它最初有三个系列,下面的代码效果很好。然而,我现在被要求添加两个在次轴上以不同方式缩放的新系列。我已经添加了范围,但我无法使用 Excel 互操作添加辅助轴。

以前有没有人这样做过,如果有,我还缺少什么?

 ChartObjects xlCharts = (Excel.ChartObjects)mSheet.ChartObjects(Type.Missing);

 ChartObject myChart = (Excel.ChartObject)xlCharts.Add(358, (double)xlsRange.Top, 650, 350);
 myChart.Name = "myCool_Chart";
 Chart chartPage = myChart.Chart;
 chartPage.ChartType = XlChartType.xlLine;
 Series series = myChart.Chart.SeriesCollection().Add(dSheet.Range["$F:$H4,$P:$Q4"]);//F thru H is left axis and P thru Q should be secondary axis            
 series.XValues = dSheet.Range["$C:$C4"];// Quart and Year values on bottom axis            
 chartPage.SeriesCollection(1).Name = "My first series";
 chartPage.SeriesCollection(2).Name = "My Second Series";
 chartPage.SeriesCollection(2).Format.Line.ForeColor.RGB = (int)XlRgbColor.rgbDarkOrange;            
 chartPage.SeriesCollection(3).Name = "My Third Series";
 chartPage.SeriesCollection(4).Name = "My fourth Series"; //this series should be secondary
 chartPage.SeriesCollection(5).Name = "My fifth Series";  //this series should be secondary

您可以使用 AxisGroup 属性:

chartPage.SeriesCollection(4).Name = "My fourth Series"; //this series should be secondary
chartPage.SeriesCollection(5).Name = "My fifth Series";  //this series should be secondary
chartPage.SeriesCollection(4).AxisGroup = XlAxisGroup.xlSecondary //2
chartPage.SeriesCollection(5).AxisGroup = XlAxisGroup.xlSecondary //2