none
Operation on ppt RRS feed

  • Question


  • I am developing  a project operate ppt files , in C#,generate a ppt directly after processing the data.But when I am add the charts, i can only add the chart frame, I need to input data then the chart can be correct. How to add chart to excel with data without input?

    This is my code:

    private void chartButton_Click(object sender, EventArgs e)
     {
            string path = pptPathTextBox.Text;
            OpenPPT(path);
            //Add a new slide
            int count = presentation.Slides.Count;
            presentation.Slides.Add(count + 1, PPT.PpSlideLayout.ppLayoutBlank);
    
            //Add a pie chart
            slide = presentation.Slides[count + 1];
            Graph.Chart aChart = (Graph.Chart)slide.Shapes.AddOLEObject(150, 150, 480, 320,"MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "",MsoTriState.msoFalse).OLEFormat.Object;
            aChart.ChartType = Graph.XlChartType.xl3DPie;
            aChart.Legend.Position = Graph.XlLegendPosition.xlLegendPositionBottom;
            aChart.HasTitle = true;
            aChart.ChartTitle.Text = "It is a pie chart";
     }

    Wednesday, April 8, 2015 3:41 AM

Answers

  • Hi JWX123,

    Which version of PowerPoint are you using? As far as I konw, we can use Shapes.AddChart to insert a chart into a slide when we use PowerPoint 2007 and PowerPoint 2010 and use Shapes.AddChart2 method in PowerPoint 2013.

    Then we can get the workbook associate with the chart by Chart.ChartData.Workbook. And here is an example that change the data in the first worksheet in the workbook:

    Application.ActivePresentation.Slides(1).Shapes(1).Chart.ChartData.Workbook.worksheets(1).Range("B2") = 10

    Also we can use Chart.SetSourceData Method to set the source data range for the chart. The following example sets the source data range for the first chart in the active document.

    With ActiveDocument.InlineShapes(1)
    
        If .HasChart Then
    
            .Chart.SetSourceData _
    
                Source:="='Sheet1'!$A$1:$D$5", _
    
                PlotBy:=xlColumns
    
        End If
    
    End With
    

    You can get more detail about chart object in PowerPoint from link below:
    Chart Object (PowerPoint)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 9, 2015 3:30 AM
    Moderator

All replies

  • Try to use this Excel library, it can generate and edit many kinds of charts, add charts to powerpoint code here:

    private void btnRun_Click(object sender, EventArgs e)
    {
        //create PPT document
        Presentation presentation = new Presentation();
    
        //set background Image
        string ImageFile = "bg.png";
        RectangleF rect2 = new RectangleF(0, 0, presentation.SlideSize.Size.Width, presentation.SlideSize.Size.Height);
        presentation.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect2);
        presentation.Slides[0].Shapes[0].Line.FillFormat.SolidFillColor.Color = Color.FloralWhite;
    
        //insert chart
        RectangleF rect = new RectangleF(presentation.SlideSize.Size.Width / 2 - 200, 100, 400, 400);
        IChart chart = presentation.Slides[0].Shapes.AppendChart(Spire.Presentation.Charts.ChartType.Cylinder3DClustered, rect);
    
        //add chart Title
        chart.ChartTitle.TextProperties.Text = "Report";
        chart.ChartTitle.TextProperties.IsCentered = true;
        chart.ChartTitle.Height = 30;
        chart.HasTitle = true;
    
        //load data from XML file to datatable
        DataTable dataTable = LoadData();
        //load data from datatable to chart
        InitChartData(chart, dataTable);
        chart.Series.SeriesLabel = chart.ChartData["B1", "D1"];
        chart.Categories.CategoryLabels = chart.ChartData["A2", "A7"];
        chart.Series[0].Values = chart.ChartData["B2", "B7"];
        chart.Series[0].Fill.FillType = FillFormatType.Solid;
        chart.Series[0].Fill.SolidColor.KnownColor = KnownColors.Brown;
        chart.Series[1].Values = chart.ChartData["C2", "C7"];
        chart.Series[1].Fill.FillType = FillFormatType.Solid;
        chart.Series[1].Fill.SolidColor.KnownColor = KnownColors.Green;
        chart.Series[2].Values = chart.ChartData["D2", "D7"];
        chart.Series[2].Fill.FillType = FillFormatType.Solid;
        chart.Series[2].Fill.SolidColor.KnownColor = KnownColors.Orange;
        //set the 3D rotation
        chart.RotationThreeD.XDegree = 10;
        chart.RotationThreeD.YDegree = 10;
        //save the document
        presentation.SaveToFile("chart.pptx", FileFormat.Pptx2010);
        System.Diagnostics.Process.Start("chart.pptx");
    
    }
    
    //function to load data from XML file to DataTable
    private DataTable LoadData()
    {
        DataSet ds = new DataSet();
        ds.ReadXmlSchema("data-schema.xml");
        ds.ReadXml("data.xml");
        return ds.Tables[0];
    }
    
    //function to load data from DataTable to IChart
    private void InitChartData(IChart chart, DataTable dataTable)
    {
        for (int c = 0; c < dataTable.Columns.Count; c++)
        {
            chart.ChartData[0, c].Text = dataTable.Columns[c].Caption;
        }
        for (int r = 0; r < dataTable.Rows.Count; r++)
        {
            object[] data = dataTable.Rows[r].ItemArray;
            for (int c = 0; c < data.Length; c++)
            {
                chart.ChartData[r + 1, c].Value = data[c];
            }
        }
    }

    Wednesday, April 8, 2015 3:44 AM
  • Hi JWX123,

    Which version of PowerPoint are you using? As far as I konw, we can use Shapes.AddChart to insert a chart into a slide when we use PowerPoint 2007 and PowerPoint 2010 and use Shapes.AddChart2 method in PowerPoint 2013.

    Then we can get the workbook associate with the chart by Chart.ChartData.Workbook. And here is an example that change the data in the first worksheet in the workbook:

    Application.ActivePresentation.Slides(1).Shapes(1).Chart.ChartData.Workbook.worksheets(1).Range("B2") = 10

    Also we can use Chart.SetSourceData Method to set the source data range for the chart. The following example sets the source data range for the first chart in the active document.

    With ActiveDocument.InlineShapes(1)
    
        If .HasChart Then
    
            .Chart.SetSourceData _
    
                Source:="='Sheet1'!$A$1:$D$5", _
    
                PlotBy:=xlColumns
    
        End If
    
    End With
    

    You can get more detail about chart object in PowerPoint from link below:
    Chart Object (PowerPoint)

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, April 9, 2015 3:30 AM
    Moderator