I ran into a little problem. I've created a code that creates a Excel sheet with a few records and creates a line chart. But the problem is I cant seem to position the chart at a specified position. Here is my code:
我遇到了一个小问题。我创建了一个代码,用于创建包含少量记录的Excel工作表并创建折线图。但问题是我似乎无法将图表定位在指定位置。这是我的代码:
excelApp.ActiveSheet.Shapes.AddChart.Select();
excelApp.ActiveChart.ChartType = Excel.XlChartType.xlLine;
excelApp.ActiveChart.SetSourceData(Source: excelApp.Range["Sheet1!$A$1:$B$7"]);
excelApp.ActiveChart.Parent.Name = "mainChartView";
excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100;
// or excelApp.ActiveChart.Shapes.Item("mainChartView").Top = 100;
Both return the following error:
两者都返回以下错误:
ArgumentExecption was unhandled (The item with the specified name wasn't found).
This is the error line:
这是错误行:
excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100;
// or excelApp.ActiveChart.Shapes.Item("mainChartView").Top = 100;
I really hope that one of you has the correct awnser, help is very appreciated!
我真的希望你们中的一个拥有正确的芒果,非常感谢帮助!
edit: Solution was:
编辑:解决方案是:
excelApp.ActiveSheet.Shapes.Item("mainChartView").Top = 20;
3 个解决方案
#1
1
You should also be able to use:
您还应该能够使用:
excelApp.ActiveChart.Parent.Top = 100;
excelApp.ActiveChart.Parent.Top = 100;
#2
1
i've found the anwser myself.
我自己找到了anwser。
I replaced:
我换了:
excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100;
with:
有:
excelApp.ActiveSheet.Shapes.Item("mainChartView").Top = 20;
#3
0
You're setting the .Parent Name property, shouldn't you be setting the name of the Chart object itself? Do you have the Chart object available? Set its name property
您正在设置.Parent Name属性,您不应该设置Chart对象本身的名称吗?你有Chart对象吗?设置其名称属性
excelApp.ActiveSheet.Shapes.AddChart should return a shape type (in your case chart). Set that types name property and then try
excelApp.ActiveSheet.Shapes.AddChart应返回一个形状类型(在您的案例图表中)。设置类型名称属性,然后尝试
#1
1
You should also be able to use:
您还应该能够使用:
excelApp.ActiveChart.Parent.Top = 100;
excelApp.ActiveChart.Parent.Top = 100;
#2
1
i've found the anwser myself.
我自己找到了anwser。
I replaced:
我换了:
excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100;
with:
有:
excelApp.ActiveSheet.Shapes.Item("mainChartView").Top = 20;
#3
0
You're setting the .Parent Name property, shouldn't you be setting the name of the Chart object itself? Do you have the Chart object available? Set its name property
您正在设置.Parent Name属性,您不应该设置Chart对象本身的名称吗?你有Chart对象吗?设置其名称属性
excelApp.ActiveSheet.Shapes.AddChart should return a shape type (in your case chart). Set that types name property and then try
excelApp.ActiveSheet.Shapes.AddChart应返回一个形状类型(在您的案例图表中)。设置类型名称属性,然后尝试