3

大家好,我正在做一个项目,我必须在单击按钮时使用 openxml将一些数据导出到 ppt 。这是我的 aspx 页面代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DocumentFormat.OpenXml.Presentation;
using ODrawing = DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml;
using DocumentFormat.Extensions1;
using DocumentFormat.OpenXml.Packaging;
using System.IO;

namespace TableInPPT
{
    public partial class _Default1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            string templateFile = Server.MapPath("~/Template/Sample.potm");
            string presentationFile = Server.MapPath("~/Template/SmapleNew.pptx");

            PotxToPptx(templateFile, presentationFile);
            //using (PresentationDocument themeDocument = PresentationDocument.Open(templateFile, false))
            using (PresentationDocument prstDoc = PresentationDocument.Open(presentationFile, true))
            {
                AddImage(prstDoc);
                AddTable(prstDoc);

            }
            string itemname = "SmapleNew.pptx";
            Response.Clear();

            Response.ContentType = "pptx";
            Response.AddHeader("Content-Disposition", "attachment; filename=" + itemname + "");
            Response.BinaryWrite(System.IO.File.ReadAllBytes(presentationFile));
            Response.Flush();
            Response.End();
        }
        private  void PotxToPptx(string templateFile, string presentationFile)
        {

            MemoryStream presentationStream = null;

            using (Stream tplStream = File.Open(templateFile, FileMode.Open, FileAccess.Read))
            {
                presentationStream = new MemoryStream((int)tplStream.Length);
                tplStream.Copy(presentationStream);
                presentationStream.Position = 0L;
            }

            using (PresentationDocument pptPackage = PresentationDocument.Open(presentationStream, true))
            {
                pptPackage.ChangeDocumentType(DocumentFormat.OpenXml.PresentationDocumentType.Presentation);
                PresentationPart presPart = pptPackage.PresentationPart;
                presPart.PresentationPropertiesPart.AddExternalRelationship("http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate",
                   new Uri(templateFile, UriKind.RelativeOrAbsolute));

                presPart.Presentation.Save();
            }

            File.WriteAllBytes(presentationFile, presentationStream.ToArray());
        }


        private void AddTable(PresentationDocument prstDoc)
        {

            // Add one slide
            Slide slide = prstDoc.PresentationPart.InsertSlide1("Custom Layout", 2);
            Shape tableShape = slide.CommonSlideData.ShapeTree.ChildElements.OfType<Shape>()
                                    .Where(sh => sh.NonVisualShapeProperties.NonVisualDrawingProperties.Title.Value == "TableHolder").SingleOrDefault();
            if (tableShape == null) return;

            // Create Graphic Frame
            OpenXmlCompositeElement gElement = GetGraphicFrame(tableShape);

            // Create a (6x3)Table
            ODrawing.Table openXmlTable = GetSmapleTable();

            // add table to graphic element
            gElement.GetFirstChild<ODrawing.Graphic>().GetFirstChild<ODrawing.GraphicData>().Append(openXmlTable);

            slide.CommonSlideData.ShapeTree.Append(gElement);
            slide.CommonSlideData.ShapeTree.RemoveChild<Shape>(tableShape);

            prstDoc.PresentationPart.Presentation.Save();
        }
        private void AddImage(PresentationDocument prstDoc)
        {
            string imgpath = Server.MapPath("~/Template/xxxx.jpg");
            Slide slide = prstDoc.PresentationPart.InsertSlide("Title and Content", 2);
            Shape titleShape = slide.CommonSlideData.ShapeTree.AppendChild(new Shape());
            titleShape.NonVisualShapeProperties = new NonVisualShapeProperties
           (new NonVisualDrawingProperties() { Id = 2, Name = "Title" },
           new NonVisualShapeDrawingProperties(new ODrawing.ShapeLocks() { NoGrouping = true }),
           new ApplicationNonVisualDrawingProperties(new PlaceholderShape() { Type = PlaceholderValues.Title }));
            titleShape.ShapeProperties = new ShapeProperties();

            // Specify the text of the title shape.
            titleShape.TextBody = new TextBody(new ODrawing.BodyProperties(),
                    new ODrawing.ListStyle(),
                    new ODrawing.Paragraph(new ODrawing.Run(new ODrawing.Text() { Text = "Trade Promotion Graph " })));
            Shape shape = slide.CommonSlideData.ShapeTree.Elements<Shape>().FirstOrDefault(
                sh => sh.NonVisualShapeProperties.NonVisualDrawingProperties.Name.Value.ToLower().Equals("Content Placeholder 2".ToLower()));
            Picture pic = slide.AddPicture(shape, imgpath);
            slide.CommonSlideData.ShapeTree.RemoveChild<Shape>(shape);
            slide.Save();
            prstDoc.PresentationPart.Presentation.Save();
        }
        private ODrawing.Table GetSmapleTable()
        {

            ODrawing.Table table = new ODrawing.Table();

            ODrawing.TableProperties tableProperties = new ODrawing.TableProperties();
            ODrawing.TableStyleId tableStyleId = new ODrawing.TableStyleId();
            tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
            //tableStyleId.Text = "{D27102A9-8310-4765-A935-A1911B00CA55}";

            tableProperties.Append(tableStyleId);

            ODrawing.TableGrid tableGrid = new ODrawing.TableGrid();
            ODrawing.GridColumn gridColumn1 = new ODrawing.GridColumn() { Width = 2600000L};
            ODrawing.GridColumn gridColumn2 = new ODrawing.GridColumn() { Width = 2600000L };
            //ODrawing.GridColumn gridColumn3 = new ODrawing.GridColumn() { Width = 1071600L };
            //ODrawing.GridColumn gridColumn4 = new ODrawing.GridColumn() { Width = 1571600L };
            //ODrawing.GridColumn gridColumn5 = new ODrawing.GridColumn() { Width = 771600L };
            //ODrawing.GridColumn gridColumn6 = new ODrawing.GridColumn() { Width = 1071600L };

            tableGrid.Append(gridColumn1);
            tableGrid.Append(gridColumn2);
            //tableGrid.Append(gridColumn3);
            //tableGrid.Append(gridColumn4);
            //tableGrid.Append(gridColumn5);
            //tableGrid.Append(gridColumn6);

            table.Append(tableProperties);
            table.Append(tableGrid);
            for (int row = 1; row <= 5; row++)
            {
                string text1 = "PARAMETERS";
                string text2="VALUES";
                if (row == 2)
                {
                    text1 =Label1.Text ;
                    text2 = TextBox1.Text;
                }
                if (row == 3)
                {
                    text1 = Label2.Text;
                    text2 = TextBox2.Text;
                }
                if (row == 4)
                {
                    text1 = Label3.Text;
                    text2 = TextBox3.Text;
                }
                if (row == 5)
                {
                    text1 = Label4.Text;
                    text2 = TextBox4.Text;
                }
                ODrawing.TableRow tableRow = new ODrawing.TableRow() { Height = 370840L };
                tableRow.Append(new ODrawing.TableCell(
                                    new ODrawing.TextBody(
                                        new ODrawing.BodyProperties(),
                                    new ODrawing.Paragraph(
                                        new ODrawing.Run(
                                            new ODrawing.RunProperties() {Language = "en-US", Dirty = false, SmartTagClean = false , FontSize = 3000},                                         
                                            new ODrawing.Text(text1)))),
                                    new ODrawing.TableCellProperties()));

                tableRow.Append(new ODrawing.TableCell(
                                   new ODrawing.TextBody(
                                       new ODrawing.BodyProperties(),
                                   new ODrawing.Paragraph(
                                       new ODrawing.Run(
                                            new ODrawing.RunProperties() {Language = "en-US", Dirty = false, SmartTagClean = false , FontSize = 3000 }, 
                                           new ODrawing.Text(text2)))),
                                   new ODrawing.TableCellProperties()));

                ODrawing.SolidFill solidFill = new ODrawing.SolidFill();

                ODrawing.SchemeColor schemeColor = new ODrawing.SchemeColor() { Val = ODrawing.SchemeColorValues.Accent6 };
                ODrawing.LuminanceModulation luminanceModulation = new ODrawing.LuminanceModulation() { Val = 75000 };

                schemeColor.Append(luminanceModulation);

                solidFill.Append(schemeColor);

                table.Append(tableRow);
            }
            //for (int row = 1; row <= 3; row++)
            //{
                     //  ODrawing.TableRow tableRow = new ODrawing.TableRow() { Height = 370840L };

            //    for (int column = 1; column <= 6; column++)
            //    {
            //        ODrawing.TableCell tableCell = new ODrawing.TableCell();

            //        TextBody textBody = new TextBody() { BodyProperties = new ODrawing.BodyProperties(), ListStyle = new ODrawing.ListStyle() };

            //        ODrawing.Paragraph paragraph = new ODrawing.Paragraph();

            //        ODrawing.Run run = new ODrawing.Run();
            //        ODrawing.RunProperties runProperties = new ODrawing.RunProperties() { Language = "en-US", Dirty = false, SmartTagClean = false };
            //        ODrawing.Text text = new ODrawing.Text();
            //        text.Text = "Smaple Text";

            //        run.Append(runProperties);
            //        run.Append(text);
            //        ODrawing.EndParagraphRunProperties endParagraphRunProperties = new ODrawing.EndParagraphRunProperties() { Language = "en-US", Dirty = false };

            //        paragraph.Append(run);
            //        paragraph.Append(endParagraphRunProperties);

            //        textBody.Append(paragraph);

            //        ODrawing.TableCellProperties tableCellProperties = new ODrawing.TableCellProperties();

            //        ODrawing.SolidFill solidFill = new ODrawing.SolidFill();

            //        ODrawing.SchemeColor schemeColor = new ODrawing.SchemeColor() { Val = ODrawing.SchemeColorValues.Accent6 };
            //        ODrawing.LuminanceModulation luminanceModulation = new ODrawing.LuminanceModulation() { Val = 75000 };

            //        schemeColor.Append(luminanceModulation);

            //        solidFill.Append(schemeColor);

            //        tableCellProperties.Append(solidFill);

            //        tableCell.Append(textBody);
            //        tableCell.Append(tableCellProperties);

            //        tableRow.Append(tableCell);
            //        if (column == 1 && row == 1)
            //        {
                       // tableRow.Append(CreateTextCell("category"));
            //        }
            //    }


           // }

            return table;
        }
        static ODrawing.TableCell CreateTextCell(string text)
        {
            ODrawing.TableCell tc = new ODrawing.TableCell(
                                new ODrawing.TextBody(
                                    new ODrawing.BodyProperties(),
                                new ODrawing.Paragraph(
                                    new ODrawing.Run(
                                        new ODrawing.Text(text)))),
                                new ODrawing.TableCellProperties());

            return tc;
        }

        private static OpenXmlCompositeElement GetGraphicFrame(Shape refShape)
        {
            GraphicFrame graphicFrame = new GraphicFrame();
            int contentPlaceholderCount = 0;
            UInt32Value graphicFrameId = 1000;
            NonVisualGraphicFrameProperties nonVisualGraphicFrameProperties = new NonVisualGraphicFrameProperties();
            NonVisualDrawingProperties nonVisualDrawingProperties = new NonVisualDrawingProperties()
            {
                Id = ++graphicFrameId,
                Name = "Table" + contentPlaceholderCount.ToString(),
            };

            NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties = new NonVisualGraphicFrameDrawingProperties();
            ODrawing.GraphicFrameLocks graphicFrameLocks = new ODrawing.GraphicFrameLocks() { NoGrouping = true };
            nonVisualGraphicFrameDrawingProperties.Append(graphicFrameLocks);
            ApplicationNonVisualDrawingProperties applicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties();
            PlaceholderShape placeholderShape = new PlaceholderShape() { Index = graphicFrameId };

            applicationNonVisualDrawingProperties.Append(placeholderShape);
            nonVisualGraphicFrameProperties.Append(nonVisualDrawingProperties);
            nonVisualGraphicFrameProperties.Append(nonVisualGraphicFrameDrawingProperties);
            nonVisualGraphicFrameProperties.Append(applicationNonVisualDrawingProperties);

            Transform transform = new Transform()
            {
                Offset = new ODrawing.Offset() { X = refShape.ShapeProperties.Transform2D.Offset.X, Y = refShape.ShapeProperties.Transform2D.Offset.Y },
                Extents = new ODrawing.Extents() { Cx = refShape.ShapeProperties.Transform2D.Extents.Cx, Cy = refShape.ShapeProperties.Transform2D.Extents.Cy }
            };

            ODrawing.Graphic graphic = new ODrawing.Graphic();
            ODrawing.GraphicData graphicData = new ODrawing.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/table" };
            graphic.Append(graphicData);
            graphicFrame.Append(nonVisualGraphicFrameProperties);
            graphicFrame.Append(transform);
            graphicFrame.Append(graphic);
            return graphicFrame;
        }
    }
}

请注意,使用的模板是仅包含两张幻灯片的示例模板,即一张标题幻灯片和另一张空白面,上面有一个文字艺术,上面写着一些随机文本。

该表还填充了来自 aspx 页面上的 4 个文本框的数据。

这是课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Packaging;
using System.IO;
using DocumentFormat.OpenXml.Presentation;
using ODrawing = DocumentFormat.OpenXml.Drawing;
using Drawing = DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml;

namespace DocumentFormat.Extensions1
{
    public static class Extensions1
    {


        internal static Slide InsertSlide(this PresentationPart presentationPart, string layoutName, int absolutePosition)
        {
            int slideInsertedPostion = 0;

            UInt32 slideId = 256U;
            slideId += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count());
            Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
            SlidePart sPart = presentationPart.AddNewPart<SlidePart>();
            slide.Save(sPart);

            SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId());
            newSlideId.Id = slideId;
            newSlideId.RelationshipId = presentationPart.GetIdOfPart(sPart);
            slideInsertedPostion = presentationPart.SlideParts.Count();
            presentationPart.Presentation.Save();

            var returnVal = ReorderSlides(presentationPart, slideInsertedPostion - 1, absolutePosition - 1);
            return GetSlideByRelationshipId(presentationPart, newSlideId.RelationshipId);
        }
        public static Slide InsertSlide1(this PresentationPart presentationPart, string layoutName, int absolutePosition)
        {
            int slideInsertedPostion = 0;

            UInt32 slideId = 256U;
            slideId += Convert.ToUInt32(presentationPart.Presentation.SlideIdList.Count());
            Slide slide = new Slide(new CommonSlideData(new ShapeTree()));
            SlidePart sPart = presentationPart.AddNewPart<SlidePart>();
            slide.Save(sPart);
            SlideMasterPart smPart = presentationPart.SlideMasterParts.First();
            SlideLayoutPart slPart = smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName));
            //SlideLayoutPart slPart = smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName));
            sPart.AddPart<SlideLayoutPart>(slPart);
            sPart.Slide.CommonSlideData = (CommonSlideData)smPart.SlideLayoutParts.SingleOrDefault(sl => sl.SlideLayout.CommonSlideData.Name.Value.Equals(layoutName)).SlideLayout.CommonSlideData.Clone();
            SlideId newSlideId = presentationPart.Presentation.SlideIdList.AppendChild<SlideId>(new SlideId());
            newSlideId.Id = slideId;
            newSlideId.RelationshipId = presentationPart.GetIdOfPart(sPart);
            slideInsertedPostion = presentationPart.SlideParts.Count();
            presentationPart.Presentation.Save();

            var returnVal = ReorderSlides(presentationPart, slideInsertedPostion - 1, absolutePosition - 1);
            return GetSlideByRelationshipId(presentationPart, newSlideId.RelationshipId);

        }
        internal static int ReorderSlides(PresentationPart presentationPart, int currentSlidePosition, int newPosition)
        {
            int returnValue = -1;

            if (newPosition == currentSlidePosition) { return returnValue; }
            int slideCount = presentationPart.SlideParts.Count();
            if (slideCount == 0) { return returnValue; }
            int maxPosition = slideCount - 1;
            CalculatePositions(ref currentSlidePosition, ref newPosition, maxPosition);
            if (newPosition != currentSlidePosition)
            {
                DocumentFormat.OpenXml.Presentation.Presentation presentation = presentationPart.Presentation;
                SlideIdList slideIdList = presentation.SlideIdList;
                SlideId sourceSlide = (SlideId)(slideIdList.ChildElements[currentSlidePosition]);
                SlideId targetSlide = (SlideId)(slideIdList.ChildElements[newPosition]);
                sourceSlide.Remove();
                if (newPosition > currentSlidePosition)
                {
                    slideIdList.InsertAfter(sourceSlide, targetSlide);
                }
                else
                {
                    slideIdList.InsertBefore(sourceSlide, targetSlide);
                }
                returnValue = newPosition;
            }
            presentationPart.Presentation.Save();
            return returnValue;
        }

        private static void CalculatePositions(ref int originalPosition, ref int newPosition, int maxPosition)
        {
            if (originalPosition < 0)
            {
                originalPosition = maxPosition;
            }
            if (newPosition < 0)
            {
                newPosition = maxPosition;
            }
            if (originalPosition > maxPosition)
            {
                originalPosition = maxPosition;
            }
            if (newPosition > maxPosition)
            {
                newPosition = maxPosition;
            }
        }
        private static Slide GetSlideByRelationshipId(PresentationPart presentationPart, DocumentFormat.OpenXml.StringValue relId)
        {
            SlidePart slidePart = presentationPart.GetPartById(relId) as SlidePart;
            if (slidePart != null)
            {
                return slidePart.Slide;
            }
            else
            {
                return null;
            }
        }
        internal static Picture AddPicture(this Slide slide, Shape referingShape, string imageFile)
        {
            Picture picture = new Picture();

            string embedId = string.Empty;
            UInt32Value picId = 10001U;
            string name = string.Empty;

            if (slide.Elements<Picture>().Count() > 0)
            {
                picId = ++slide.Elements<Picture>().ToList().Last().NonVisualPictureProperties.NonVisualDrawingProperties.Id;
            }
            name = "image" + picId.ToString();
            embedId = "rId" + (slide.Elements<Picture>().Count() + 915).ToString(); // some value

            NonVisualPictureProperties nonVisualPictureProperties = new NonVisualPictureProperties()
            {
                NonVisualDrawingProperties = new NonVisualDrawingProperties() { Name = name, Id = picId, Title = name },
                NonVisualPictureDrawingProperties = new NonVisualPictureDrawingProperties() { PictureLocks = new Drawing.PictureLocks() { NoChangeAspect = true } },
                ApplicationNonVisualDrawingProperties = new ApplicationNonVisualDrawingProperties() { UserDrawn = true }
            };

            BlipFill blipFill = new BlipFill() { Blip = new Drawing.Blip() { Embed = embedId } };
            Drawing.Stretch stretch = new Drawing.Stretch() { FillRectangle = new Drawing.FillRectangle() };
            blipFill.Append(stretch);

            ShapeProperties shapeProperties = new ShapeProperties()
            {
                Transform2D = new Drawing.Transform2D()
                {
                    Offset = new Drawing.Offset() { X = 1554691, Y = 1600200 },
                    Extents = new Drawing.Extents() { Cx = 6034617, Cy = 4525963 }
                }
            };
            Drawing.PresetGeometry presetGeometry = new Drawing.PresetGeometry() { Preset = Drawing.ShapeTypeValues.Rectangle };
            Drawing.AdjustValueList adjustValueList = new Drawing.AdjustValueList();

            presetGeometry.Append(adjustValueList);
            shapeProperties.Append(presetGeometry);
            picture.Append(nonVisualPictureProperties);
            picture.Append(blipFill);
            picture.Append(shapeProperties);

            slide.CommonSlideData.ShapeTree.Append(picture);

            // Add Image part
            slide.AddImagePart(embedId, imageFile);

            slide.Save();
            return picture;
        }
        private static void AddImagePart(this Slide slide, string relationshipId, string imageFile)
        {
            ImagePart imgPart = slide.SlidePart.AddImagePart(GetImagePartType(imageFile), relationshipId);
            using (FileStream imgStream = File.Open(imageFile, FileMode.Open))
            {
                imgPart.FeedData(imgStream);
            }
        }
        private static ImagePartType GetImagePartType(string imageFile)
        {
            string[] imgFileSplit = imageFile.Split('.');
            string imgExtension = imgFileSplit.ElementAt(imgFileSplit.Count() - 1).ToString().ToLower();
            if (imgExtension.Equals("jpg"))
                imgExtension = "jpeg";
            return (ImagePartType)Enum.Parse(typeof(ImagePartType), imgExtension, true);
        }
        public static void Copy(this Stream source, Stream target)
        {
            if (source != null)
            {
                MemoryStream mstream = source as MemoryStream;
                if (mstream != null) mstream.WriteTo(target);
                else
                {
                    byte[] buffer = new byte[2048];
                    int length = buffer.Length, size;
                    while ((size = source.Read(buffer, 0, length)) != 0)
                        target.Write(buffer, 0, size);
                }
            }
        }
    }
}

现在这是我的查询: 1.在 aspx 页面中,如果我尝试用我自己的模板(与示例相同)替换 templateFile,它会在这一行给我一个错误对象引用未设置为对象的实例” (sh => sh.NonVisualShapeProperties.NonVisualDrawingProperties.Title.Value == "TableHolder")在 AddTable.Otherwise 它工作正常。

2.Also 在第二张幻灯片中,我得到了所需的表格,但在页面顶部写了 5 次“图像”一词。

3.同样在生成的powerpoint文件中,除了第一张和最后一张幻灯片(即模板中的幻灯片),模板样式不显示在幻灯片上。

谢谢你。

4

0 回答 0