簡體   English   中英

C#如何在PowerPoint幻燈片中添加多個對象

[英]c# how to add multiple objects in power point slide

1)我必須在幻燈片的頂部中心位置顯示一個文本

2)在文本下方,我必須在表中顯示數據

3)我必須從存儲在數據表中的數據在同一張幻燈片中生成折線圖。

我正在尋找這種輸出。 在這里,我分享一個屏幕截圖。 在此處輸入圖片說明

在這里,我將分享我到目前為止的代碼。

    pptNS.ApplicationClass powerpointApplication = null;
    pptNS.Presentation pptPresentation = null;
    pptNS.Slide pptSlide = null;
    pptNS.ShapeRange shapeRange = null;
    pptNS.Shape oShape = 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\NPS.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");

        System.Array myvalues = (System.Array)destRange.Cells.Value;
        List<Tuple<string, string>> cellData = ConvertToStringArray(myvalues);

        int iRows = cellData.Count;
        int iColumns = 2;
        int row = 1;

        oShape = pptSlide.Shapes.AddTable(iRows, iColumns, 500, 110, 160, 120);

        foreach (Tuple<string, string> item in cellData)
        {
            string strdate = item.Item1;
            string strValue = item.Item2;

            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Text = strdate;
            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Name = "Verdana";
            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Size = 8;
            oShape.Table.Cell(row, 1).Shape.Fill.BackColor.RGB = System.Drawing.Color.FromArgb(255, 0, 0).ToArgb(); 
            oShape.Table.Cell(row, 1).Shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Text = (strValue.StartsWith("0") ?  "0%" : (strValue + "0%"));
            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Name = "Verdana";
            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Size = 8;


            row++;
        }

        oShape.Top = 10;
        oShape.Left =10;

        //copy range
        //destRange.Copy();

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

        //var table = pptSlide.Shapes.AddTable();
        // 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();

    }
}

1)問題是我不知道如何以編程方式在幻燈片的頂部中心添加自己的文本?

2)這樣,我添加了一張表格來滑動我的數據。 這是代碼

        List<Tuple<string, string>> cellData = ConvertToStringArray(myvalues);

        int iRows = cellData.Count;
        int iColumns = 2;
        int row = 1;

        oShape = pptSlide.Shapes.AddTable(iRows, iColumns, 500, 110, 160, 120);

        foreach (Tuple<string, string> item in cellData)
        {
            string strdate = item.Item1;
            string strValue = item.Item2;

            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Text = strdate;
            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Name = "Verdana";
            oShape.Table.Cell(row, 1).Shape.TextFrame.TextRange.Font.Size = 8;
            oShape.Table.Cell(row, 1).Shape.Fill.BackColor.RGB = System.Drawing.Color.FromArgb(255, 0, 0).ToArgb(); 
            oShape.Table.Cell(row, 1).Shape.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;

            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Text = (strValue.StartsWith("0") ?  "0%" : (strValue + "0%"));
            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Name = "Verdana";
            oShape.Table.Cell(row, 2).Shape.TextFrame.TextRange.Font.Size = 8;


            row++;
        }

        oShape.Top = 10;
        oShape.Left =10;

3)現在我必須將折線圖添加到幻燈片中,數據存儲在數據表中,但是我不知道如何將折線圖添加到幻燈片中。

所以請有人幫我完成編碼。

謝謝

您正在尋找將形狀添加到幻燈片。 我看到您在哪里創建形狀,但從未添加它們。 以下是先前有關添加圖表和標題的幾個問題。 希望它能有所幫助,並祝您好運。

創建折線圖

添加標題和圖像

暫無
暫無

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

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