Is it possible to update a PowerPoint slide with new data (in C#)?

跟風遠走 提交于 2019-12-29 06:33:29

问题


Are there any examples out there on how to update a PowerPoint slide (by clearing the text in a certain textbox and updating it with new content)?

I have a monthly report to generate in PowerPoint and I have all the data in a database. I am trying to determine if I can simply autogenerate the slides by having a blank PowerPoint template with three textboxes and the data would get filled in from my C# code.

The other examples i would look for are:

  • bulletted lists
  • tables

Any help in the right direction would be appreciated. I see a few questions on SOF that are similar but none seems to answer this question.

I assume the easiest would be to use the OpenXML format (.pptx) as i am running on a web server that may not have PowerPoint on the machine.


回答1:


Yeah this is possible and here is a tutorial on how to do it online. They also have the sample code for the project in that blog which should help.




回答2:


Sorry for the really late delay, hope you're still looking for this. Note this does NOT use the SDK - it just uses System.IO.Packagingand Linq (and XML Literals). Anyway, here's what to do:

  1. Create a presentation. On slide 3, add 4 textboxes.
  2. Put text in three of them and name them "Sample1", "Sample2" and "Sample3".
  3. In the last textbox, put two lines of text and then makes those lines bullet points. Name it "ListSample1".

That's all you need. Then save the file and note the path, and change the filePath variable below to reflect your presentation's path.

Run the below in a Console app:

Imports System.IO
Imports System.IO.Packaging ''# Add reference to WindowsBase for this
Imports <xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
Imports <xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
Imports <xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
Module Module1
    Public Const documentRelationshipType As String = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
    Sub Main()
        Dim slide, document As XElement
        Dim pptPackage As Package = Nothing
        Dim slidePart, documentPart As PackagePart
        Dim filePath As String = "C:\Users\Me\Documents\visual studio 2010\Projects\FillPowerPoint\FillPowerPoint\sample.pptx"

        pptPackage = Package.Open(filePath, FileMode.Open, FileAccess.ReadWrite)
        Using pptPackage
            Dim documentRelationship As PackageRelationship = pptPackage.GetRelationshipsByType(documentRelationshipType).FirstOrDefault
            Dim documentUri As Uri = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), documentRelationship.TargetUri)
            documentPart = pptPackage.GetPart(documentUri)
            document = XElement.Load(New StreamReader(documentPart.GetStream))

            Dim slideList = From e In document.<p:sldIdLst>.<p:sldId>
            Dim slideIndex As Integer = 3 ''# this is the slide number we want, 1-based
            Dim slideReference As String = slideList(slideIndex - 1).@r:id.ToString
            slidePart = pptPackage.GetPart(PackUriHelper.ResolvePartUri(documentPart.Uri, documentPart.GetRelationship(slideReference).TargetUri))
            slide = XElement.Load(New StreamReader(slidePart.GetStream))

            ''# Replace just text value in Sample1 textbox
            Dim Sample1 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "Sample1" Select e.<p:txBody>.<a:p>.<a:r>.<a:t>.SingleOrDefault
            Sample1.Value = "new text in sample 1"

            ''# Replace text and make bold inn Sample2 textbox
            Dim Sample2 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "Sample2" Select e.<p:txBody>.<a:p>.<a:r>.SingleOrDefault
            Sample2.<a:rPr>.SingleOrDefault.Add(New XAttribute("b", 1))
            Sample2.<a:t>.SingleOrDefault.Value = "new bold text in sample 2"

            ''# Replace text and make bold inn Sample2 textbox
            Dim Sample3 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "Sample3" Select e.<p:txBody>.SingleOrDefault
            Sample3.<a:p>.Remove()
            Dim newParagraphs As XElement = <placeholder>
                                                <a:p>
                                                    <a:r>
                                                        <a:rPr lang="en-US" dirty="0" smtClean="0"/>
                                                        <a:t>Sample3</a:t>
                                                    </a:r>
                                                </a:p>
                                                <a:p>
                                                    <a:r>
                                                        <a:rPr lang="en-US" smtClean="0"/>
                                                        <a:t>With a new paragraph</a:t>
                                                    </a:r>
                                                    <a:endParaRPr lang="en-US" dirty="0"/>
                                                </a:p>
                                            </placeholder>
            Sample3.SingleOrDefault.Add(newParagraphs.Elements)

            ''# Create a new list of bullets
            Dim s() As String = {"Bullet 1", "Bullet 2", "Bullet 3"}
            Dim ListSample1 = From e In slide.<p:cSld>.<p:spTree>.<p:sp> Where e.<p:nvSpPr>.<p:cNvPr>.@name = "ListSample1" Select e.<p:txBody>.SingleOrDefault
            ListSample1.<a:p>.Remove()
            ListSample1.SingleOrDefault.Add(From e In s Select <a:p>
                                                                   <a:pPr marL="285750" indent="-285750">
                                                                       <a:buFont typeface="Arial" pitchFamily="34" charset="0"/>
                                                                       <a:buChar char="•"/>
                                                                   </a:pPr>
                                                                   <a:r>
                                                                       <a:rPr lang="en-US" dirty="0" smtClean="0"/>
                                                                       <a:t><%= e %></a:t>
                                                                   </a:r>
                                                               </a:p>)
            slide.Save(slidePart.GetStream)
        End Using
    End Sub
End Module

Sorry, I know this is heavily weighted towards VB and XML Literals, but C# should be able to do the same thing with some conversion work.




回答3:


You probably want to have a look at the Office Automation API. Will let you programatically modify, create, etc powerpoint documents.

This document is for an older version of powerpoint but the same process works for newer versions. http://support.microsoft.com/default.aspx?scid=kb;EN-US;303718

Word of warning though, if you do use office automation api make sure you build it against the lowest version of office you want to support with this tool you are creating in c#.



来源:https://stackoverflow.com/questions/3903142/is-it-possible-to-update-a-powerpoint-slide-with-new-data-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!