Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Excel中使用C#代码获取次轴?_C#_Excel - Fatal编程技术网

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

如何在Excel中使用C#代码获取次轴?,c#,excel,C#,Excel,我使用了这段代码,有人建议我如何使用C#在Excel图表中获取次轴吗?我是从Excel选项中手动获取的,但不是用C代码 我知道我有点晚了,但也许这会帮助别人 我正在尝试为我的项目在图表上做一些格式化,因此我还做了一些事情,但您可能正在寻找的部分如下: Excel.Axis verticalAxisSecondary = (Excel.Axis)chart.Axes(Excel.XlAxisType.xlValue, Excel.XlAxisGroup.xlSecondary); 只有一个来之不

我使用了这段代码,有人建议我如何使用C#在Excel图表中获取次轴吗?我是从Excel选项中手动获取的,但不是用C代码


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

我正在尝试为我的项目在图表上做一些格式化,因此我还做了一些事情,但您可能正在寻找的部分如下:

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

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

我正在尝试为我的项目在图表上做一些格式化,因此我还做了一些事情,但您可能正在寻找的部分如下:

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