1

I want embed a excel file to powerpoint file:

I had try this code, but it can't add object:

   // Open the source document as read/write. 
    using (var presentationDocument = PresentationDocument.Open(strFile, true))
    {
        
        var presentationPart = presentationDocument.PresentationPart;
        var newSlidePart = GetSlidePartsInOrder( presentationPart).Last();
       
            
        string datafile = @"F:\AUTOM\t1.xlsx";
      
        ShapeTree tree = newSlidePart.Slide.CommonSlideDatShapeTree;       
           
        GraphicFrame graphicFrame = new GraphicFrame();
        var embedId = "rId" + (newSlidePart.Slide.Elements().Count() + 1915);
        var nonVisualPictureProperties = new NonVisualGraphicFrameProperties(
           new NonVisualDrawingProperties { Id = (UInt32Value)4U, Name = "Chart 3" },
           new NonVisualGraphicFrameDrawingProperties(),
           new ApplicationNonVisualDrawingProperties());


        var blipFill = new BlipFill();
        var blip = new Blip { Embed = embedId };

        // Creates an BlipExtensionList instance and adds its children 
        var blipExtensionList = new BlipExtensionList();
        var blipExtension = new BlipExtension { Uri = "{28A0092B-C50C-407E-A947-70E740481C1D}" };

        var useLocalDpi = new UseLocalDpi { Val = false };
        useLocalDpi.AddNamespaceDeclaration("a14", "http://schemas.microsoft.com/office/drawing/2010/main");

        blipExtension.Append(useLocalDpi);
        blipExtensionList.Append(blipExtension);
        bliAppend(blipExtensionList);

        var stretch = new Stretch();
        var fillRectangle = new FillRectangle();
        stretch.Append(fillRectangle);

        blipFill.Append(blip);
        blipFill.Append(stretch);

        // Create new Embedded Package Part    
        EmbeddedPackagePart embPackage = newSlidePart.AddNewPart<EmbeddedPackagePart>("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", embedId);
        // Feed imported data from an excel file into the embedded package               
        embPackage.FeedData(new FileStream(datafile, FileMode.Open, FileAccess.ReadWrite));

       
        var aspectRatio = 1;

        // Compute the image's offset on the page (in x and y), and its width cx and height cy.
        // Note that sizes are expressed in EMU (English Metric Units)
        // const int emusPerCm = 360000;
        var cy = 5029200L;
        var cx = (long)(cy * aspectRatio);
        if (cx > 8229600L)
        {
            cx = 8229600L;
            cy = (long)(cx / aspectRatio);
        }

        // Creates an ShapeProperties instance and adds its children. 
        var shapeProperties = new ShapeProperties();
        var transform2D = new Transform2D();
        var offset = new Offset { X = (9144000L - cx) / 2, Y = 1524000L };
        var extents = new Extents { Cx = cx, Cy = cy };

        transform2D.Append(offset);
        transform2D.Append(extents);

        var presetGeometry = new PresetGeometry { Preset = ShapeTypeValues.Rectangle };
        var adjustValueList = new AdjustValueList();

        presetGeometry.Append(adjustValueList);

        shapeProperties.Append(transform2D);
        shapeProperties.Append(presetGeometry);

        graphicFrame.Append(nonVisualPictureProperties);
        graphicFrame.Append(blipFill);
        graphicFrame.Append(shapeProperties);

        tree.AppendChild(graphicFrame);
     
        // Save the modified presentation.
        presentationPart.Presentation.Save();

       
    }

How can embed a excel file to PowerPoint file by OpenXML?

D T
  • 3,522
  • 7
  • 45
  • 89

0 Answers0