简体   繁体   English

如何使用C#在Excel电子表格中重新放置X轴标签

[英]How to reposition the X Axis label in an excel spread sheet using C#

I've posted this question in the MS forums, but the only response (create a macro and extrapolate from the VBA code) didn't help. 我已经在MS论坛上发布了这个问题,但是唯一的回答(创建宏并从VBA代码推断)没有帮助。 The only vague hint I got was to use TickLabelPosition -- which apparently is something only useful if you're using VB, and I'm using C# 我得到的唯一模糊的提示是使用TickLabelPosition-显然,这仅在您使用VB且我使用C#时才有用

What I'm trying to do SHOULD be the most simple and common thing in the world: I just want to move the X Axes label to the bottom of the chart. 我正在尝试做的事情应该是世界上最简单和常见的事情:我只想将X轴标签移到图表的底部。 How hard could that be? 那有多难? Well, I'll admit I'm not overly experienced in either C# or the Excel interop, but currently it seems impossible. 好吧,我承认我对C#或Excel互操作并没有过多的经验,但是目前看来这是不可能的。

I am writing a C# program (VS2010) to create Excel spreadsheets from imported data. 我正在编写一个C#程序(VS2010)从导入的数据创建Excel电子表格。 I have no problem actually creating the spread sheet with all the correct data and ranges. 我实际上可以使用所有正确的数据和范围来创建电子表格。 What I CANNOT figure out is how to move the label for the X Axis. 我无法弄清楚如何移动X轴的标签。 It's got to be something simple that I'm missing, but the thing always appears right at the zero line and since my values go negative, that means it's right in the middle of the chart. 我一定缺少一个简单的东西,但是它总是出现在零线处,并且由于我的值变为负数,这意味着它在图表的中间。 Now I can get the thing to where I want it to be if I use a template, but I don't want to do that. 现在,如果使用模板,我可以将其放到想要的位置,但是我不想这样做。

Here is a code snippit: 这是一个代码片段:

         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.ApplyChartTemplate(@“ Chart1.crtx”); (<- template) (<-模板)

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

Using this puts the Horizontal Value axis in the middle of the chart, since the x axis goes negative. 由于x轴变为负数,因此使用此选项将“水平值”轴置于图表的中间。 Now I can move this MANUALLY, or by using a chart template (as I did), but that's not the best plan. 现在,我可以手动移动此图,也可以使用图表模板(如我所做的那样)移动,但这并不是最好的计划。 The best plan would be to move it programmatically, but I can't find a way to do it. 最好的计划是以编程方式移动它,但我找不到解决方法。 I can find a way to move the LEGEND (chart.Legend) but not the Value. 我可以找到一种方法来移动“传奇”(chart.Legend),但不能移动“值”。

Thanks for any help. 谢谢你的帮助。

尽管完全没有文档说明,但此命令将起作用:chart.Axes(2).CrossesAt = chart.Axes(2).MinimumScale;

If you want to move the X Axis labels to the "Low" position, here is the code in VBA which might help: 如果要将X轴标签移动到“低”位置,以下是VBA中的代码,可能会有所帮助:

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: 如果要移动整个X轴(标签和刻度),请在此处使用该代码的VBA代码:

chart.Axes(xlValue).CrossesAt = -15

where -15 is the minimum number in the Y range. 其中-15是Y范围内的最小值。 Notice the we're actually modifying the Y Axis here: 注意,我们实际上是在这里修改Y轴:

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). 我意识到您要求使用C#代码,但这应该很容易转换为C#(而且我没有方便的Visual Studio项目来测试Excel Interop)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM