【问题标题】:C#: How to copy excel cell range and chart data to power point slidesC#:如何将excel单元格范围和图表数据复制到power point幻灯片
【发布时间】:2017-01-31 22:39:56
【问题描述】:

我在 Excel 工作表中有数据和图表,我需要在运行时将其从 Excel 复制到 power point 幻灯片。

我有一个运行良好的代码,但该代码只能将图表数据复制到 Excel 工作表而不是范围数据。

请查看我的 Excel 屏幕截图。所以任何人都知道我的工作表中的数据是如何存在的,我需要以编程方式将其复制到 PowerPoint 幻灯片中。

这是我用来将范围数据和图表数据动态复制到 PowerPoint 的代码。

private void Form1_Load(object sender, EventArgs e)
{
    pptNS.ApplicationClass powerpointApplication = null;
    pptNS.Presentation pptPresentation = null;
    pptNS.Slide pptSlide = null;
    pptNS.ShapeRange shapeRange = null;

    xlNS.ApplicationClass excelApplication = null;
    xlNS.Workbook excelWorkBook = null;
    xlNS.Worksheet targetSheet = null;
    xlNS.ChartObjects chartObjects = null;
    xlNS.ChartObject existingChartObject = null;
    xlNS.Range destRange = null;

    string paramPresentationPath = @"D:\test\Chart Slide.pptx";
    string paramWorkbookPath = @"D:\test\MyExcelData.xlsx";
    object paramMissing = Type.Missing;


    try
    {
        // Create an instance of PowerPoint.
        powerpointApplication = new pptNS.ApplicationClass();

        // Create an instance Excel.          
        excelApplication = new xlNS.ApplicationClass();

        // Open the Excel workbook containing the worksheet with the chart
        // data.
        excelWorkBook = excelApplication.Workbooks.Open(paramWorkbookPath,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing, paramMissing,
                        paramMissing, paramMissing);

        // Get the worksheet that contains the chart.
        targetSheet =
            (xlNS.Worksheet)(excelWorkBook.Worksheets["Spain"]);

        // Get the ChartObjects collection for the sheet.
        chartObjects =
            (xlNS.ChartObjects)(targetSheet.ChartObjects(paramMissing));



        // Create a PowerPoint presentation.
        pptPresentation = powerpointApplication.Presentations.Add(
                            Microsoft.Office.Core.MsoTriState.msoTrue);

        // Add a blank slide to the presentation.
        pptSlide =
            pptPresentation.Slides.Add(1, pptNS.PpSlideLayout.ppLayoutBlank);

        // capture range
        //var writeRange = targetSheet.Range["A1:B15"];
        destRange = targetSheet.get_Range("A1:B15");
        //copy range
        destRange.Copy();

        // Paste the chart into the PowerPoint presentation.
        shapeRange = pptSlide.Shapes.Paste();


        // Position the chart on the slide.
        shapeRange.Left = 60;
        shapeRange.Top = 100;

        // Get or capture the chart to copy.
        existingChartObject =(xlNS.ChartObject)(chartObjects.Item(1));


        // Copy the chart from the Excel worksheet to the clipboard.
        existingChartObject.Copy();

        // Paste the chart into the PowerPoint presentation.
        shapeRange = pptSlide.Shapes.Paste();
        //Position the chart on the slide.
        shapeRange.Left = 90;
        @shapeRange.Top = 100;

        // Save the presentation.
        pptPresentation.SaveAs(paramPresentationPath,
                        pptNS.PpSaveAsFileType.ppSaveAsOpenXMLPresentation,
                        Microsoft.Office.Core.MsoTriState.msoTrue);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    finally
    {
        // Release the PowerPoint slide object.
        shapeRange = null;
        pptSlide = null;

        // Close and release the Presentation object.
        if (pptPresentation != null)
        {
            pptPresentation.Close();
            pptPresentation = null;
        }

        // Quit PowerPoint and release the ApplicationClass object.
        if (powerpointApplication != null)
        {
            powerpointApplication.Quit();
            powerpointApplication = null;
        }

        // Release the Excel objects.
        targetSheet = null;
        chartObjects = null;
        existingChartObject = null;

        // Close and release the Excel Workbook object.
        if (excelWorkBook != null)
        {
            excelWorkBook.Close(false, paramMissing, paramMissing);
            excelWorkBook = null;
        }

        // Quit Excel and release the ApplicationClass object.
        if (excelApplication != null)
        {
            excelApplication.Quit();
            excelApplication = null;
        }

        GC.Collect();
        GC.WaitForPendingFinalizers();

    }
}

请查看我的代码并让我知道要在我的代码中纠正什么,因为结果单元格范围和图表我都可以复制到 power point 幻灯片。

【问题讨论】:

    标签: c# excel powerpoint excel-interop


    【解决方案1】:

    我怀疑它像复制粘贴那么简单。您可能需要先在 PowerPoint 幻灯片中创建一个表格,并将表格值设置为表格中的 RangeValue。我熟悉 PowerPoint 互操作,但它可能看起来像:

    var table = pptSlide.Shapes.AddTable();
    destRange = targetSheet.get_Range("A1:B15");
    for (int i = 1; i <= destRange.Rows; i++) 
    { 
        for (int j = 1; j <= destRange.Columns; j++) 
        {
            table.Table.Cell(i, j).Shape.TextFrame.TextRange.Text =destRange[i, j].Text;
        } 
    } 
    

    【讨论】:

    • 如何通过指定 left 和 top 来定位幻灯片中的表格......任何提示。我会测试你的代码并让你知道。谢谢
    • @Mou 抱歉,我不熟悉 PowerPoint 库。试试pptSlide.Shapes.AddTable() 看到here
    • 我不知道你从哪里得到代码来填充表格,但是当我粘贴你的代码时它不起作用。我检查了我必须在 for 循环中填充表格单元格,这不是我的意图。
    • made4dotnet.com/tabid/141/aid/16/… 看看人们如何循环填充表格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多