简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM