C#使用Excel Interop添加辅助轴
我构build和应用程序将从SQL数据库创build一个Excel电子表格。 它原来有三个系列,下面的代码很好。 不过,我现在被要求增加两个新的系列,在次要的轴线上有不同的缩放比例。 我已经添加了范围,但是我不能使用Excel interop添加辅助轴。
有没有人做过这个,如果是的话,我错过了什么?
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
您可以使用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