1

我已经在 MS 论坛上发布了这个问题,但唯一的回应(创建一个宏并从 VBA 代码推断)没有帮助。我得到的唯一模糊提示是使用 TickLabelPosition - 这显然只有在您使用 VB 时才有用,而我正在使用 C#

我正在尝试做的应该是世界上最简单和最常见的事情:我只想将 X Axes 标签移动到图表的底部。那能有多难?好吧,我承认我在 C# 或 Excel 互操作方面都没有过多的经验,但目前这似乎是不可能的。

我正在编写一个 C# 程序(VS2010)来从导入的数据创建 Excel 电子表格。实际上,我没有问题创建具有所有正确数据和范围的电子表格。我不知道如何移动 X 轴的标签。我错过了一些简单的东西,但它总是出现在零线上,而且由于我的值变为负数,这意味着它就在图表的中间。现在,如果我使用模板,我可以将它放到我想要的位置,但我不想那样做。

这是一个代码片段:

         const string topLeft = "A9";
        const string bottomRight = "B18";
        const string graphTitle = "Graph Title";
        const string xAxis = "Time";
        const string yAxis = "Value";
        string chartTabName;


        var range = workSheet.get_Range(topLeft, bottomRight);    

// Add chart.
var charts = workSheet.ChartObjects() as
    Microsoft.Office.Interop.Excel.ChartObjects;


var chartObject = charts.Add(20, 20, 750, 500) as
    Microsoft.Office.Interop.Excel.ChartObject;
var chart = chartObject.Chart;

// Set chart range.

chart.SetSourceData(range);

chart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlXYScatterSmooth;

// chart.ApplyChartTemplate(@"Chart1.crtx"); (<- 模板)

chart.ChartWizard(Source: range,
    Title: graphTitle,
    CategoryTitle: xAxis,
    ValueTitle: yAxis);

使用它会将水平值轴置于图表的中间,因为 x 轴变为负数。现在我可以手动移动它,或者使用图表模板(就像我一样),但这不是最好的计划。最好的计划是以编程方式移动它,但我找不到办法。我可以找到一种方法来移动 LEGEND(chart.Legend)而不是 Value。

谢谢你的帮助。

4

2 回答 2

1

虽然它完全没有记录,但这个命令可以工作:chart.Axes(2).CrossesAt = chart.Axes(2).MinimumScale;

于 2012-06-07T13:57:12.537 回答
0

If you want to move the X Axis labels to the "Low" position, here is the code in VBA which might help:

chart.Axes(xlCategory).TickLabelPosition = xlLow

And the constants are defined as:

Const xlCategory = 1
Const xlLow = -4134 (&HFFFFEFDA)

If you want to move the whole X Axis (labels and ticks), here the VBA code for that:

chart.Axes(xlValue).CrossesAt = -15

where -15 is the minimum number in the Y range. Notice the we're actually modifying the Y Axis here:

Const xlValue = 2

I realized you asked for C# code, but this should be easy to translate to C# (and I didn't have a Visual Studio project handy to test Excel Interop).

于 2012-06-06T21:41:24.210 回答