Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 423
Nelrum Avatar asked Oct 05 '22 13:10

Nelrum


1 Answers

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
like image 116
Rohit Avatar answered Oct 19 '22 15:10

Rohit