5

我有一个 C# 代码构建一个包含几个图表的 excel 文件。我正在使用 Excel 到Microsoft.Office.Interop.Excel12。

这很好,但同时我需要生成一个包含相同图表的 PowerPoint 演示文稿。

有没有办法复制 Microsoft.Office.Interop.Excel.ChartObject到 PowerPoint 文件?或者以任何其他方式以编程方式将这些图表复制到 PowerPoint 演示文稿中?

4

1 回答 1

4

我想这应该工作

            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
于 2013-04-02T12:31:35.117 回答