簡體   English   中英

C#:如何將Excel單元格范圍和圖表數據復制到Powerpoint幻燈片

[英]C#: How to copy excel cell range and chart data to power point slides

我在Excel工作表中有數據和圖表,我需要在運行時將它們從Excel復制到PowerPoint幻燈片。

我有一個工作正常的代碼,但是該代碼只能將圖表數據復制到Excel工作表,而不能將范圍數據復制到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();

    }
}

請查看我的代碼,並讓我知道要在我的代碼中糾正哪些內容,結果單元格范圍和圖表都可以復制到Powerpoint幻燈片。

我懷疑它像復制粘貼一樣簡單。 您可能需要先在PowerPoint幻燈片中創建一個表,然后將表值設置為該表中的Range Value 我對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;
    } 
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM