简体   繁体   English

C#使用Excel Interop添加辅助轴

[英]C# Adding secondary Axis using Excel Interop

I built and application that will create an Excel spreadsheet from an SQL database. 我构建了一个应用程序,它将通过SQL数据库创建一个Excel电子表格。 It originally had three series and the following code works great. 它最初有三个系列,下面的代码效果很好。 However, I am now being asked to add two new series that scale differently in a secondary axis. 但是,现在要求我添加两个在辅助轴上缩放比例不同的新系列。 I've added the ranges but I cannot add the secondary axis using Excel interop. 我已经添加了范围,但是无法使用Excel互操作添加辅助轴。

Has anyone done this before and if so what am I missing? 之前有人做过吗?

 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$2:$H$124,$P$2:$Q$124"]);//F thru H is left axis and P thru Q should be secondary axis            
 series.XValues = dSheet.Range["$C$2:$C$124"];// 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

You can use the AxisGroup property: 您可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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