Nelrum
Nelrum

Reputation: 113

How to copy chart from an excel file to power point?

I have a C# code building an excel file containing few charts. I'm working with Excel through Microsoft.Office.Interop.Excel 12.

That's working fine but at the same time I need to generate a PowerPoint presentation containing the same exact charts.

Is there any way to copy Microsoft.Office.Interop.Excel.ChartObject to PowerPoint file? Or any other way to copy those charts in to PowerPoint presentation programmatically?

Upvotes: 5

Views: 4173

Answers (1)

Rohit
Rohit

Reputation: 10236

I guess this should work

            using xlNS = Microsoft.Office.Interop.Excel;
            using Microsoft.Office.Interop.PowerPoint;

            xlNS.ApplicationClass excelApplication = null;
            xlNS.Workbook excelWorkBook = null;
            xlNS.Worksheet targetSheet = null;
            xlNS.ChartObjects chartObjects = null;
            xlNS.ChartObject existingChartObject = null;
            Microsoft.Office.Interop.PowerPoint.ShapeRange shapeRange = null;
            Microsoft.Office.Interop.PowerPoint.Slide CurSlide;


            excelApplication = new xlNS.ApplicationClass();//Create New Excel
            excelWorkBook = excelApplication.Workbooks.Open(Excelpath,
                 paramMissing, paramMissing, paramMissing, paramMissing, paramMissing,
                 paramMissing, paramMissing, paramMissing, paramMissing, paramMissing,
                 paramMissing, paramMissing, paramMissing, paramMissing);

            Ws = (Excel.Worksheet)excelWorkBook.Worksheets[6];//Your Sheet that contain Chart
            Ws.Activate();
            targetSheet = (xlNS.Worksheet)(excelWorkBook.Worksheets["SheetName"]);
            chartObjects = (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));
            existingChartObject = (xlNS.ChartObject)(chartObjects.Item(1));

            existingChartObject.Copy();
            shapeRange = CurSlide.Shapes.Paste();//Paste it to your Current Slide
            shapeRange.Left = 435;
            shapeRange.Top = 80; //Formatting your chart

Upvotes: 4

Related Questions