如何在Excel中使用C#代码获取辅助轴?

时间:2016-02-11 09:22:41

标签: c# excel

我使用过这段代码,有人建议我如何使用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");

1 个答案:

答案 0 :(得分:0)

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

我正在尝试在我的项目的图表上进行一些格式化,所以我正在做更多的事情,但你可能正在寻找的部分是这样的:

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);
        }
    }
}