0

我正在尝试将图表图像导出到 PPT,即一张幻灯片中的每个图像,下面是我的代码

 String strTemplate, strPic;
            strTemplate = "C:\\Program Files (x86)\\Microsoft Office\\Templates\\Presentation Designs\\Maple.GIF";

            //strPic = @"C:\Users\rongala.ganesh\Pictures\arrow_left_green_large.png";

            bool bAssistantOn;

            Microsoft.Office.Interop.PowerPoint.Application objApp;
            Microsoft.Office.Interop.PowerPoint.Presentations objPresSet;
            Microsoft.Office.Interop.PowerPoint._Presentation objPres;
            Microsoft.Office.Interop.PowerPoint.Slides objSlides;
            Microsoft.Office.Interop.PowerPoint._Slide objSlide;
            Microsoft.Office.Interop.PowerPoint.TextRange objTextRng;
            Microsoft.Office.Interop.PowerPoint.Shapes objShapes;
            Microsoft.Office.Interop.PowerPoint.Shape objShape;
            Microsoft.Office.Interop.PowerPoint.SlideShowWindows objSSWs;
            Microsoft.Office.Interop.PowerPoint.SlideShowTransition objSST;
            Microsoft.Office.Interop.PowerPoint.SlideShowSettings objSSS;
            Microsoft.Office.Interop.PowerPoint.SlideRange objSldRng;
            //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(strTemplate,
                 MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
            objSlides = objPres.Slides;
            //Build Slide #1:
            //Add text to the slide, change the font and insert/position a
            //picture on the first slide.
            objSlide = objSlides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
            objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
            // objTextRng.Text = "FAME Presentation";
            objTextRng.Font.Name = "Comic Sans MS";
            objTextRng.Font.Size = 25;
            foreach (var ar in arr)
            {
                // ScriptManager.RegisterClientScriptBlock(this.Page,typeof(string),"alert"
                objSlide = objSlides.Add(1, Microsoft.Office.Interop.PowerPoint.PpSlideLayout.ppLayoutTitleOnly);
                objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
                // objTextRng.Text = "FAME Presentation";
                objTextRng.Font.Name = "Comic Sans MS";
                objTextRng.Font.Size = 25;
                string[] str = (string[])ar;
                strPic = str[0];
                objSlide.Shapes.AddPicture(strPic, MsoTriState.msoFalse, MsoTriState.msoTrue,
                      150, 150, 500, 350);
                objTextRng = objSlide.Shapes[1].TextFrame.TextRange;
                objTextRng.Text = str[1];
                objTextRng.Font.Name = "Comic Sans MS";
                objTextRng.Font.Size = 48;

                //Build Slide #2:
                //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.

                //Build Slide #3:
                //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.
            }



        }
        catch (Exception ex)
        {

            throw ex;
        }

当我运行它时,localHost 中的一切都运行良好,但是当我托管此应用程序 IIS7 时,它抛出异常 PowerPoint 无法打开文件。所以我认为最好添加响应头,所以我按照下面的代码

dt是 DataTable 名称,其中包含我单击 SaveImage 时保存的图像的路径

 GridView1.AllowPaging = false;

            GridView1.DataSource = dt;
            GridView1.DataBind();////////write this code only if paging is enabled.

            Response.Clear();

            Response.AddHeader("content-disposition", "attachment;filename=FileName.ppt");///////for text file write FileName.txt

            Response.Charset = "";
            // If you want the option to open the Excel file without saving than

            // comment out the line below
            // Response.Cache.SetCacheability(HttpCacheability.NoCache);

            Response.ContentType = "application/vnd.ppt";//for text file write vnd.txt



            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite =

            new HtmlTextWriter(stringWrite);
            GridView1.RenderControl(htmlWrite);

            Response.Write(stringWrite.ToString());

            Response.End();

在这里我可以将图像添加到 PPT 中,但它们在同一张幻灯片中,并且图像相互重叠。

4

1 回答 1

1

MS 不支持在服务器上使用互操作(如 ASP.NET) - 请参阅http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

由于 Windows Vista MS 引入了一些与安全相关的措施,这些措施会阻止 Windows 服务执行“类似桌面”的操作......这意味着您必须绕过一些安全措施才能使其正常工作(不推荐!)。

要在服务器场景中处理 PPT,有一些选项(免费和商业):

一个免费选项(尽管仅适用于较新的 pptx 格式!)例如 来自 MS 的 OpenXML 2

一个商业选项是Aspose.Slides,它可以处理旧 (PPT) 和新 (PPTX) 格式。

于 2013-04-01T14:44:44.870 回答