以编程方式编辑嵌入式图表,但未在运行时添加

时间:2013-10-17 22:52:47

标签: vb.net visual-studio-2010 excel charts

在我的一个Excel模板中,有一个预设图表可自动填充从自动表中获取的数据。

一切正常。我遇到问题的部分是如何在我的代码中引用该图表。

我能收集到的最好的东西是使用像Main.xlBook.Charts(6)这样的东西,其中6是xlBook中图表的索引。

    Dim chart As Excel.Chart = CType(Main.xlBook.Charts(6), Excel.Chart)
    chart.Axes(Excel.XlCategoryType.xlTimeScale).MinimumScale = Main.xLSheet1.Cells(2, 1).value / 86277
    chart.Axes(Excel.XlCategoryType.xlTimeScale).MaximumScale = Main.xLSheet1.Cells(radios.Count + 1, 1).value / 86277

使用上面的代码,当我尝试设置对象时,它会中断。我不想在运行时制作图表,因为在大多数情况下我根本不需要编辑图表。我自动化的方式,它在95%的时间内正确填充。我只需要能够调整另外5%的范围。

那么有没有办法在xlbook中引用一个未在运行时生成的图表对象?

ps,作为第二个问题,如何在选项严格打开的情况下更改轴? (我目前的方式不是选项严格友好)

一如既往地谢谢你们


更新


想出来

    Dim xlCharts As Excel.ChartObjects = CType(Main.xlSheet3.ChartObjects, Excel.ChartObjects)
    Dim myChart As Excel.ChartObject = xlCharts.Item(5)
    Dim chartPage As Excel.Chart = myChart.Chart
    chartPage.Axes(Excel.XlAxisType.xlCategory).minimumscale = Main.xLSheet1.Cells(2, 1).value
    chartPage.Axes(Excel.XlAxisType.xlCategory).maximumscale = Main.xLSheet1.Cells(radios.Count + 1, 1).value

2 个答案:

答案 0 :(得分:0)

在原始代码中设置图表的名称。然后你可以按名称调用图表:

Charts("Sales").Activate

答案 1 :(得分:0)

为了使用minimumscale和maximumscale,您可以使用chartobject并设置图表

Dim xlCharts As Excel.ChartObjects = CType(Main.xlSheet3.ChartObjects, Excel.ChartObjects)
Dim myChart As Excel.ChartObject = xlCharts.Item(5)
Dim chartPage As Excel.Chart = myChart.Chart
chartPage.Axes(Excel.XlAxisType.xlCategory).minimumscale = Main.xLSheet1.Cells(2, 1).value
chartPage.Axes(Excel.XlAxisType.xlCategory).maximumscale = Main.xLSheet1.Cells(radios.Count + 1, 1).value

希望这有帮助