2010-10-11 26 views
3

有沒有關於如何更新PowerPoint幻燈片的任何示例(通過清除某個文本框中的文本並用新內容更新它)?是否有可能使用新數據(使用C#)更新PowerPoint幻燈片?

我有一個月度報告在PowerPoint中生成,我擁有數據庫中的所有數據。我試圖確定是否可以簡單地通過具有三個文本框的空白PowerPoint模板自動生成幻燈片,並且數據將從我的C#代碼中填充。

其他的例子,我會去找有:

  • 採用項目符號列表

在正確的方向任何幫助,將不勝感激。我在SOF上看到幾個類似的問題,但似乎沒有人回答這個問題。

我認爲最簡單的方法就是使用OpenXML格式(.pptx),因爲我在可能沒有在機器上安裝PowerPoint的Web服務器上運行。

+0

它必須是一個C#示例嗎?它可以是vb.net嗎? – 2010-10-11 15:24:30

+0

@Otaku - vb。網很好。 .i總是可以轉換 – leora 2010-10-12 01:00:05

回答

2

是的這是可能的,here是如何在線做的教程。他們也有該博客中項目的示例代碼,這應該有所幫助。

1

您可能想看看Office自動化API。將讓您以編程方式修改,創建PowerPoint等文檔。

本文檔適用於較舊版本的powerpoint,但相同的過程適用於較新版本。 http://support.microsoft.com/default.aspx?scid=kb;EN-US;303718

但是,如果您使用辦公自動化api,請確保您使用c#中創建的此工具支持的最低版本的Office支持。

+0

我會假設我會使用OpenXML API,因爲這不會要求我在機器上安裝Office – leora 2010-10-11 02:32:16

3

對不起,真的很晚推遲,希望你仍然在尋找這個。請注意,不是使用SDK - 它只是使用System.IO.Packaging和Linq(和XML Literals)。無論如何,這是做什麼:

  1. 創建一個演示文稿。在幻燈片3上, 添加4個文本框。
  2. 在其中三個文本中放置文本並將它們命名爲 「Sample1」,「Sample2」和 「Sample3」。
  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)[email protected]: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>[email protected] = "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>[email protected] = "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>[email protected] = "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>[email protected] = "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#應該能夠做同樣的事情有一些轉換工作。

相關問題