我们如何从C#设置Excel图表的位置?

时间:2022-09-28 16:49:39

I am trying to generate an Excel chart from C#. Chart is generated just find but it allways appears at the center of the screen. How can I set the position of the chart?

我正在尝试从C#生成Excel图表。生成的图表只是查找但它总是出现在屏幕的中心。如何设置图表的位置?

Thanks.

谢谢。

My code looks like this:

我的代码如下所示:

Microsoft.Office.Interop.Excel._Workbook ebook = (Microsoft.Office.Interop.Excel._Workbook)etablo.Workbooks.Add(true);

Microsoft.Office.Interop.Excel._Worksheet esheet = (Microsoft.Office.Interop.Excel._Worksheet)ebook.ActiveSheet;

_Chart grafik1 = (Chart)ebook.Charts.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

/* Add Data From Cells here */
/* Then */
grafik1.Location(XlChartLocation.xlLocationAsObject, esheet.Name);

2 个解决方案

#1


10  

after you generate a chart, you can handle chart as a shape object:

生成图表后,您可以将图表作为形状对象处理:

for example:

例如:

esheet.Shapes.Item("Chart 1").Top = 100;
esheet.Shapes.Item("Chart 1").Left = 250;


// or you can handle shape by index - indexes start from 1 so esheet.Shapes.Item(1).Top

i think, you would like to set position right the relevating cells, for example:

我认为,你想在相关的单元格中设置位置,例如:

esheet.Shapes.Item("Chart 1").Top = (float)(double)esheet.get_Range("A5","A6").Top;

so ...i hope this will help :)

所以......我希望这会有所帮助:)

Luboss

Luboss

#2


1  

When Adding chart You can use this code to handle it's left, top, width, height. use this link

添加图表时您可以使用此代码来处理它的左,上,宽,高。使用此链接

var chartObject = charts.Add(260, 30, 300, 300);

#1


10  

after you generate a chart, you can handle chart as a shape object:

生成图表后,您可以将图表作为形状对象处理:

for example:

例如:

esheet.Shapes.Item("Chart 1").Top = 100;
esheet.Shapes.Item("Chart 1").Left = 250;


// or you can handle shape by index - indexes start from 1 so esheet.Shapes.Item(1).Top

i think, you would like to set position right the relevating cells, for example:

我认为,你想在相关的单元格中设置位置,例如:

esheet.Shapes.Item("Chart 1").Top = (float)(double)esheet.get_Range("A5","A6").Top;

so ...i hope this will help :)

所以......我希望这会有所帮助:)

Luboss

Luboss

#2


1  

When Adding chart You can use this code to handle it's left, top, width, height. use this link

添加图表时您可以使用此代码来处理它的左,上,宽,高。使用此链接

var chartObject = charts.Add(260, 30, 300, 300);