开发者

C# 4.0 Excel Generate Chart and Position

开发者 https://www.devze.com 2023-02-20 11:07 出处:网络
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.

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:

 excelApp.ActiveSheet.Shapes.AddChart.Select();
 excelApp.ActiveChart.ChartType = Excel.XlChartType.xlLine;
 excelApp.ActiveC开发者_JS百科hart.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;


You should also be able to use:

excelApp.ActiveChart.Parent.Top = 100;


i've found the anwser myself.

I replaced:

excelApp.ActiveChart.Shapes.Item(excelApp.ActiveChart.Name).Top = 100;

with:

excelApp.ActiveSheet.Shapes.Item("mainChartView").Top = 20;


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

excelApp.ActiveSheet.Shapes.AddChart should return a shape type (in your case chart). Set that types name property and then try

0

精彩评论

暂无评论...
验证码 换一张
取 消