简体   繁体   中英

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. 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#

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. 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.

I am writing a C# program (VS2010) to create Excel spreadsheets from imported data. 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. 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"); (<- 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. 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.

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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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