是否可以使用新数据更新 PowerPoint 幻灯片(在 C# 中)?

发布于 2024-09-27 01:32:02 字数 370 浏览 4 评论 0原文

有没有关于如何更新 PowerPoint 幻灯片的示例(通过清除某个文本框中的文本并使用新内容更新它)?

我需要在 PowerPoint 中生成一份月度报告,并且所有数据都存储在数据库中。我试图确定是否可以通过使用带有三个文本框的空白 PowerPoint 模板来简单地自动生成幻灯片,并且数据将从我的 C# 代码中填充。

我要寻找的其他示例是:

  • 项目符号列表
  • 表格

任何正确方向的帮助将不胜感激。我在 SOF 上看到一些类似的问题,但似乎没有一个回答这个问题。

我认为最简单的方法是使用 OpenXML 格式 (.pptx),因为我在计算机上可能没有 PowerPoint 的 Web 服务器上运行。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小苏打饼 2024-10-04 01:32:02

很抱歉耽搁了很长时间,希望您仍在寻找这个。请注意,这使用SDK - 它只使用System.IO.Packaging和Linq(和XML Literals)。无论如何,要做的是:

  1. 创建演示文稿。在幻灯片 3 上,
    添加4个文本框。
  2. 在其中三个中输入文字并命名
    他们“样本1”,“样本2”和
    “样本3”。
  3. 在最后一个文本框中,输入两行
    文本,然后制作这些线条
    要点。将其命名为“ListSample1”。

这就是你所需要的。然后保存文件并记下路径,并更改下面的 filePath 变量以反映演示文稿的路径。

在控制台应用程序中运行以下代码:

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

抱歉,我知道这对 VB 和 XML 文字来说非常重要,但 C# 应该能够通过一些转换工作来完成同样的事情。

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.

遗失的美好 2024-10-04 01:32:02

是的,这是可能的,这里是有关如何在线进行操作的教程。他们还在该博客中提供了该项目的示例代码,这应该会有所帮助。

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.

夏日浅笑〃 2024-10-04 01:32:02

您可能想了解一下 Office Automation API。让您以编程方式修改、创建等 powerpoint 文档。

本文档适用于较旧版本的 powerpoint,但相同的过程适用于较新版本。
http://support.microsoft.com/default.aspx?scid =kb;EN-US;303718

不过,请注意,如果您确实使用办公自动化 api,请确保您使用您在 C# 中创建的此工具支持的最低 Office 版本来构建它。

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#.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文