1

我对使用 asp.net 将图表和网格导出到 ppt 感到震惊。在下面的代码中,我能够显示图表但不能显示网格。我的图表由两个条形系列和两个线系列组成,我无法显示,线系列应与辅助 y 轴(右侧)绑定。在这里,我提供了我的代码,因此非常感谢任何修改或任何示例代码。这是使用 Microsoft.Office.Interop.PowerPoint 开发的

在 aspx.cs 页面下面的代码中

   try
        {
            bool bAssistantOn;
            Microsoft.Office.Interop.PowerPoint.Application objApp;
            Presentations objPresSet;
            _Presentation objPres;
            Slides objSlides;
            _Slide objSlide;
            TextRange objTextRng;
            Microsoft.Office.Interop.PowerPoint.Shapes objShapes;
            Microsoft.Office.Interop.PowerPoint.Shape objShape;
            SlideShowWindows objSSWs;
            SlideShowTransition objSST;
            SlideShowSettings objSSS;
            Microsoft.Office.Interop.PowerPoint.SlideRange objSldRng;
            Microsoft.Office.Interop.Graph.Chart objChart;
            Microsoft.Office.Interop.Graph.DataSheet dataSheet;

            //Create a new presentation based on a template.
            objApp = new Microsoft.Office.Interop.PowerPoint.Application();
            objApp.Visible = MsoTriState.msoTrue;
            objPresSet = objApp.Presentations;
            objPres = objPresSet.Open(@"D:\Template.pptx", MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
            //.potx file is the template file of powerpoint.
            //Take a template file remove all it's slides and save it.
            //Then give it's physical path instead of it.
            objSlides = objPres.Slides;

            //Build Slide #1
            //Add text to the slide title, format the text. Also add a chart to the
            //slide and change the chart type to a 3D pie chart.
            objSlide = objSlides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
            objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
            objTextRng.Text = "My Chart";
            objTextRng.Font.Name = "Comic Sans MS";
            objTextRng.Font.Size = 48;

            objChart = (Microsoft.Office.Interop.Graph.Chart)objSlide.Shapes.AddOLEObject(150, 150, 480, 320, "MSGraph.Chart.8", "", MsoTriState.msoFalse, "", 0, "", MsoTriState.msoFalse).OLEFormat.Object;
            dataSheet = objChart.Application.DataSheet;

            // objChart.Legend.LegendEntries();
            //objChart.Axes(Microsoft.Office.Interop.Graph.XlAxisType.xlValue, Microsoft.Office.Interop.Graph.XlAxisGroup.xlSecondary);

            for (int i = 1; i < ds.Tables[0].Rows.Count + 1; i++)
            {
                if (i == 1)
                {
                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                        dataSheet.Cells[i, j + 1] = ds.Tables[0].Rows[j]["ProductGroup"];// x-axis values
                }
                else if (i == 2)
                {
                    //objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlBarOfPie;

                    dataSheet.Cells[i, 1] = "ActualsQTD"; // Legend 1
                    for (int j = 1; j <= ds.Tables[0].Rows.Count; j++)
                    {
                        dataSheet.Cells[i, j + 1] = Convert.ToInt32(ds.Tables[0].Rows[j - 1]["ActualsQTD"])/1000;
                    }
                }
                else if (i == 3)
                {
                    //objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlBarOfPie;

                    dataSheet.Cells[i, 1] = "ForecastQTD"; // Legend 2
                    for (int j = 1; j <= ds.Tables[0].Rows.Count; j++)
                    {
                        dataSheet.Cells[i, j + 1] = Convert.ToInt32(ds.Tables[0].Rows[j - 1]["ForecastQTD"])/1000;
                    }
                }
                else if (i == 4)
                {
                    //objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlLine;

                    dataSheet.Cells[i, 1] = "QTDRatio"; // Legend 3
                    for (int j = 1; j <= ds.Tables[0].Rows.Count; j++)
                    {
                        dataSheet.Cells[i, j + 1] = ds.Tables[0].Rows[j-1]["QTDRatio"];
                    }
                }
                else if (i == 5)
                {
                    //objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlLine;

                    dataSheet.Cells[i, 1] = "Target Attain"; // Legend 4
                    for (int j = 1; j <= ds.Tables[0].Rows.Count; j++)
                    {
                        dataSheet.Cells[i, j + 1] = ds.Tables[0].Rows[j-1]["Target Attain"];
                    }
                }

                objChart.Application.Update();
            }
            //objChart.ChartType = Microsoft.Office.Interop.Graph.XlChartType.xlLine; //changing the graph type : by default pie chart
            objChart.SeriesCollection(2);
            objChart.Legend.Position = Microsoft.Office.Interop.Graph.XlLegendPosition.xlLegendPositionBottom;
            objChart.HasTitle = true;
            objChart.ChartTitle.Text = "Chart";

            ///Build Slide #2:
            ///Change the background color of this slide only. Add a text effect to the slide
            ///and apply various color schemes and shadows to the text effect.
            objSlide = objSlides.Add(2, PpSlideLayout.ppLayoutBlank);
            objSlide.FollowMasterBackground = MsoTriState.msoFalse;
            objShapes = objSlide.Shapes;
            objShape = objShapes.AddTextEffect(MsoPresetTextEffect.msoTextEffect27, "The End", "Impact", 96, MsoTriState.msoFalse, MsoTriState.msoFalse, 230, 200);

            //Modify the slide show transition settings for all slides in
            //the presentation.
            int[] SlideIdx = new int[2];
            for (int i = 0; i < 2; i++) SlideIdx[i] = i + 1;
            objSldRng = objSlides.Range(SlideIdx);
            objSST = objSldRng.SlideShowTransition;
            objSST.AdvanceOnTime = MsoTriState.msoTrue;
            objSST.AdvanceTime = 3;
            objSST.EntryEffect = PpEntryEffect.ppEffectBoxOut;

            //Prevent Office Assistant from displaying alert messages:
            ////bAssistantOn = objApp.Assistant.On;
            ////objApp.Assistant.On = false;

            //Run the Slide show
            //objSSS = objPres.SlideShowSettings;
            //objSSS.StartingSlide = 1;
            //objSSS.EndingSlide = 2;![enter image description here][1]
            //objSSS.Run();

            //Wait for the slide show to end.
            ////objSSWs = objApp.SlideShowWindows;
            ////while (objSSWs.Count >= 1) System.Threading.Thread.Sleep(100);

            //Reenable Office Assisant, if it was on:
            ////if (bAssistantOn)
            ////{
            ////    objApp.Assistant.On = true;
            ////    objApp.Assistant.Visible = false;
            ////}

            //Close the presentation without saving changes and quit PowerPoint.
            objPres.SaveAs(@"D:\Sample", PpSaveAsFileType.ppSaveAsPresentation, MsoTriState.msoTrue);
            //"D:\Sample" is the location where the ppt will be saved.
            objPres.Close();
            objApp.Quit();
        }
        catch (Exception ex)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();

            sb.Append("<script type = 'text/javascript'>");

            sb.Append("window.onload=function(){");

            sb.Append("alert('");

            sb.Append(ex.Message);

            sb.Append("')};");

            sb.Append("</script>");

            ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());

       }

我的图表

通过上面的代码,我只能显示条形图,但不能同时显示带有辅助 y 轴的条形和线系列。

提前致谢。

4

0 回答 0