Question posted 2010 · +5 upvotes
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);
Accepted answer +7 upvotes
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
2 code variants in this answer
- Variant 1 — 5 lines, starts with
esheet.Shapes.Item("Chart 1").Top = 100; - Variant 2 — 1 lines, starts with
esheet.Shapes.Item("Chart 1").Top = (float)(double)esheet.g…
Excel VBA objects referenced (5)
Interop.Excel— Using events with Excel objectsInterop.Excel— Using Excel worksheet functions in Visual BasicMicrosoft.Office— Controlling One Microsoft Office Application from AnotherMicrosoft.Office— List the Name and Office Location of Each Manager Belonging to an Exchange Distribution ListShapes.Item— Allow users to add items to an unbound combo box
Top excel Q&A (6)
- Shortcut to Apply a Formula to an Entire Column in Excel +335 (2011)
- How should I escape commas and speech marks in CSV files so they work in Excel? +136 (2012)
- Convert xlsx to csv in linux command line +96 (2012)
- How to create a link inside a cell using EPPlus +50 (2011)
- IF statement: how to leave cell blank if condition is false ("" does not work) +44 (2013)
- T-SQL: Export to new Excel file +44 (2012)
excel solutions on this site
.