none
直接在ppt裡面插入圖表Chart RRS feed

  • 问题

  • 最近的項目需要在ppt(2013)裡面插入圖表,但是我現在只能添加圖表的外框(Frame),要顯示的話還需要手動輸入數據相應的圖表才能出來,所以大家有設麼好的方法在插入的同時就可以將數據輸入然後直接顯示圖表呢?C#,vs2012. 多謝!
    2015年4月15日 2:20

答案

  • 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];
            }
        }
    }

    2015年4月15日 2:22

全部回复

  • 最近的項目需要在ppt(2013)裡面插入圖表,但是我現在只能添加圖表的外框(Frame),要顯示的話還需要手動輸入數據相應的圖表才能出來,所以大家有設麼好的方法在插入的同時就可以將數據輸入然後直接顯示圖表呢?C#,vs2012. 多謝!
    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";
     }


    2015年4月15日 2:20
  • 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];
            }
        }
    }

    2015年4月15日 2:22