简体   繁体   English

如何使用C#在excel中命名轴?

[英]How to name axis in excel using C#?

This is how I create chart: 这是我创建图表的方式:

Excel.ChartObjects xlChartsq = (Excel.ChartObjects)xlWorkSheet.ChartObjects(Type.Missing);
Excel.ChartObject myChartq = (Excel.ChartObject)xlChartsq.Add(10, 1000, 175, 310);
Excel.Chart chartPageq = myChartq.Chart;
myChartq.Select();

And I have succesfully named primary axis this way, I have used MDSN`s code : 我已经成功命名了主轴,我使用了MDSN的代码

Excel.Axis axis = myChartq.Chart.Axes(
Excel.XlAxisType.xlValue,
Excel.XlAxisGroup.xlPrimary);

axis.HasTitle = true;
axis.AxisTitle.Text = "AXIS`s NAME";

It works as should, but then I tried to use such method to name secondary axis, it failed: 它可以正常工作,但是随后我尝试使用这种方法来命名辅助轴,但失败了:

Excel.Axis axisq = myChartq.Chart.Axes(
Excel.XlAxisType.xlValue,
Excel.XlAxisGroup.xlSecondary);

axisq.HasTitle = true; 
axisq.AxisTitle.Text = "another AXIS`s NAME";

It compiled without any errors, unfortunately after calling this code I got such error: unexpected exception " System.Runtime.InteropServices.COMException ", and something like HRESULT E_FAIL error was returned. 它编译没有任何错误,不幸的是,在调用此代码后,我得到了这样的错误:意外的异常“ System.Runtime.InteropServices.COMException ”,并且返回了类似HRESULT E_FAIL的错误。

What am I doing wrong? 我究竟做错了什么? Any suggestions? 有什么建议么?

UPD1: Before naming I create some series, but, honestly, I can't figure out how it can be associated with Secondary axis. UPD1:在命名之前,我创建了一些序列,但是老实说,我不知道如何将其与Secondary轴关联。

for (int i = 0; i < current_Excel_Position; i++ )
{
 Excel.Series series1 = seriesCollection.NewSeries();
 series1.XValues = xlWorkSheet.get_Range("A" + Convert.ToString(92 + i), "B" +     Convert.ToString(92 + i)); ;
 series1.Values = xlWorkSheet.get_Range("C" + Convert.ToString(92 + i), "D" + Convert.ToString(92 + i));
}

There has to be at least one series on the secondary axis. 次级轴上必须至少有一个系列。 If all your data is on the primary axis you cannot show / edit the secondary axis. 如果所有数据都在主轴上,则无法显示/编辑副轴。

Do you have any series on the secondary axis? 副轴上有任何系列吗? If not that is your problem. 如果不是,那是你的问题。

Edit: This is how you can set the value (Y axis in your case) and category (X axis in your case): 编辑:这是设置值(例中的Y轴)和类别(例中的X轴)的方法:

Excel.Axis axisq = myChartq.Chart.Axes(Excel.XlAxisType.xlValue,
               Excel.XlAxisGroup.xlPrimary);
axisq.HasTitle = true;
axisq.AxisTitle.Text = "Value axis NAME";

axisq = myChartq.Chart.Axes(Excel.XlAxisType.xlCategory,
                Excel.XlAxisGroup.xlPrimary);
axisq.HasTitle = true;
axisq.AxisTitle.Text = "Category axis NAME";

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

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