C#-为什么直方图在Excel 2016中不起作用? [英] C# - why Histogram does not work in Excel 2016?

查看:199
本文介绍了C#-为什么直方图在Excel 2016中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中构建了Excel 2016 vsto应用程序.我有一个图表控件,想要将图表类型设置为直方图.我可以从excel中选择此图表,但无法以编程方式设置此图表类型.

I have excel 2016 vsto application build in c#. I have a chart control and want to set chart type to the histogram. I can select this chart from excel but I am not able set this chart type programmatically.

换句话说,我无法在XlChartType枚举中找到直方图图表类型.

In other words, I am not able to find histogram chart type in the XlChartType enum.

推荐答案

在这种情况下,应始终在excel宏中打开对象浏览器"并搜索所需内容

In such cases you should always open Object Browser in excel macros and and search for what you are looking for

从对象浏览器中可以看到,直方图的值为十六进制的11876.您可以通过定义常量

As you can see from object browser, the value for histogram is 118 or 76 in hex. You can use the same in your code directly by defining a constant

代码

在下面调试您的代码时,我发现了一个问题

Debugging your code below I found a issue

Worksheet sheet1 = Globals.Factory.GetVstoObject(Globals.Sheet1.Application.Worksheets[1]);
//chartTest
Excel.ChartObject myChart = (Excel.ChartObject)sheet1.ChartObjects("chartTest");
myChart.Chart.SetSourceData(sheet1.Range["A1", "A51"]);
myChart.Chart.Type = 118;

您需要做的是将118分配给ChartType而不是Type.下面的代码对我来说很好用

What you need to do is assign 118 to ChartType and not Type. Below code worked fine for me

Worksheet sheet1 = Globals.Factory.GetVstoObject(Globals.Sheet1.Application.Worksheets[1]);
//chartTest
Excel.ChartObject myChart = (Excel.ChartObject)sheet1.ChartObjects("chartTest");
myChart.Chart.SetSourceData(sheet1.Range["A1", "A51"]);
Excel.XlChartType myType = (Excel.XlChartType)118;

myChart.Chart.ChartType = myType;

这篇关于C#-为什么直方图在Excel 2016中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆