如何在Excel中使用C#代码获得第二轴?

我用这个代码,任何人都build议我如何获得使用C#的Excel图表中的次轴? 手动我从Excel选项,但不是在C#代码。

Excel.Range chartRange; Excel.ChartObjects xlCharts = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing); Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(200, 100, 500, 250); Excel.Chart chartPage = myChart.Chart; chartRange = xlWorkSheet.get_Range("A1", "f5"); chartPage.SetSourceData(chartRange, misValue); chartPage.ChartType = Excel.XlChartType.xlColumnClustered; chartPage.Export(@"C:Desktop\excel_chart_export.bmp", "BMP", misValue); xlWorkBook.SaveAs(@"C:Desktop\csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); MessageBox.Show("Excel file created , you can find the file Desktop:\\abcd.xls"); 

我知道我有点晚了,但也许会帮助别人。

我正在为我的项目在图表上做一些格式化工作,所以我正在做更多的事情,但是您可能要查找的部分是这样的:

 Excel.Axis verticalAxisSecondary = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary); 

只有一个来之不易的信息:设置和格式化一个辅助轴后,你已经分配了一些数据,否则你会有一个发现为什么它总是崩溃的哎呀。 是的,我发现它很难:-)


作为一个附注,我的整个代码如下,如果你有兴趣:

 private static void FormatChart(Excel.Chart chart, string secondaryAxisTitle = null) { Excel.Axis horizontalAxis = null; Excel.AxisTitle horizontalAxisTitle = null; Excel.Axis verticalAxisPrimary = null; Excel.AxisTitle verticalAxisPrimaryTitle = null; Excel.Axis verticalAxisSecondary = null; Excel.AxisTitle verticalAxisSecondaryTitle = null; Excel.ChartTitle chartTitle = null; try { chart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementPrimaryCategoryAxisTitleAdjacentToAxis); horizontalAxis = chart.Axes(Excel.XlAxisType.xlCategory, Excel.XlAxisGroup.xlPrimary); horizontalAxis.HasTitle = true; horizontalAxisTitle = horizontalAxis.AxisTitle; horizontalAxisTitle.Text = "Date and Time"; horizontalAxis.HasMajorGridlines = true; chart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementPrimaryValueAxisTitleAdjacentToAxis); verticalAxisPrimary = chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlPrimary); verticalAxisPrimary.HasTitle = true; verticalAxisPrimaryTitle = verticalAxisPrimary.AxisTitle; verticalAxisPrimaryTitle.Text = "Power [MW]"; verticalAxisPrimary.CrossesAt = -1E+39; //Just move horizontal axis all way down to the bottom of the graph. if (!string.IsNullOrWhiteSpace(secondaryAxisTitle)) { chart.HasAxis[Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary] = true; chart.SetElement(Microsoft.Office.Core.MsoChartElementType.msoElementSecondaryValueAxisTitleAdjacentToAxis); verticalAxisSecondary = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary); verticalAxisSecondary.HasTitle = true; verticalAxisSecondaryTitle = verticalAxisSecondary.AxisTitle; verticalAxisSecondaryTitle.Text = secondaryAxisTitle; } if (chart.HasTitle) { chartTitle = chart.ChartTitle; chartTitle.Delete(); } } finally { if (horizontalAxis != null) { Marshal.ReleaseComObject(horizontalAxis); } if (horizontalAxisTitle != null) { Marshal.ReleaseComObject(horizontalAxisTitle); } if (verticalAxisPrimary != null) { Marshal.ReleaseComObject(verticalAxisPrimary); } if (verticalAxisPrimaryTitle != null) { Marshal.ReleaseComObject(verticalAxisPrimaryTitle); } if (verticalAxisSecondary != null) { Marshal.ReleaseComObject(verticalAxisSecondary); } if (verticalAxisSecondaryTitle != null) { Marshal.ReleaseComObject(verticalAxisSecondaryTitle); } if (chartTitle != null) { Marshal.ReleaseComObject(chartTitle); } } }