5

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?

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);
slhsen
  • 606
  • 9
  • 21
  • Could you post a super-simplified example of what you are doing now to create the chart? An empty chart would be fine. This would be a useful starting point I think. – Mark Byers Feb 03 '10 at 09:54
  • @Mark Byers Updated. These code was from an example that I have found somewhere. I am not sure if it is right way to do it though – slhsen Feb 03 '10 at 11:18

2 Answers2

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
  • 101
  • 3
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);
Saurabh Agrawal
  • 7,581
  • 2
  • 27
  • 51