2011-04-20 99 views
1

我使用代碼生成器獲得了PowerPoint中表格的代碼,但我無法將該表格添加到現有的PowerPoint文檔中。使用Open XML將表格添加到PowerPoint幻燈片

我嘗試添加另一臺到目標幻燈片,並執行以下操作:

Table table = slidePart.Slide.Descendants<Table>().First(); 
table.RemoveAllChildren(); 
Table createdTable = CreateTable(); 

foreach (OpenXmlElement childElement in createdTable.ChildElements) 
{ 
    table.AppendChild(childElement.CloneNode(true)); 
} 

但沒有奏效。

我對這個問題沒有意見。 我最初的目標是添加一個包含動態列數和固定行數的表格到我的演示文稿中。

回答

0

我知道它已經很長時間了,因爲這個問題發佈了,但以防萬一如果有人需要一個工作代碼在pptx中創建表。

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

namespace ANF.Slides.TestEngine 
{ 
class Program 
{ 
    static int index = 1; 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Preparing Presentation"); 
     PopulateData(); 
     // GeneratedClass cls=new GeneratedClass(); 
     //cls.CreatePackage(@"E:\output.pptx"); 
     Console.WriteLine("Completed Presentation"); 
     Console.ReadLine(); 
    } 

    private static void PopulateData() 
    { 
     var overflow = false; 
     const int pageBorder = 3000000; 
     var db = new AdventureWorksEntities(); 
     var products = db.Products;//.Take(5); 
     const string outputFile = @"E:\openxml\output.pptx"; 
     File.Copy(@"E:\OpenXml\Template.pptx", outputFile, true); 

     using (var myPres = PresentationDocument.Open(outputFile, true)) 
     { 
      var presPart = myPres.PresentationPart; 
      var slideIdList = presPart.Presentation.SlideIdList; 

      var list = slideIdList.ChildElements 
         .Cast<SlideId>() 
         .Select(x => presPart.GetPartById(x.RelationshipId)) 
         .Cast<SlidePart>(); 


      var tableSlidePart = (SlidePart)list.Last(); 
      var current = tableSlidePart; 
      long totalHeight = 0; 
      foreach (var product in products) 
      { 

       if (overflow) 
       { 
        var newTablePart = CloneSlidePart(presPart, tableSlidePart); 
        current = newTablePart; 
        overflow = false; 
        totalHeight = 0; 
       } 




       var tbl = current.Slide.Descendants<A.Table>().First(); 
       var tr = new A.TableRow(); 
       tr.Height = 200000; 
       tr.Append(CreateTextCell(product.Name)); 
       tr.Append(CreateTextCell(product.ProductNumber)); 
       tr.Append(CreateTextCell(product.Size)); 
       tr.Append(CreateTextCell(String.Format("{0:00}", product.ListPrice))); 
       tr.Append(CreateTextCell(product.SellStartDate.ToShortDateString())); 
       tbl.Append(tr); 


       totalHeight += tr.Height; 


       if (totalHeight > pageBorder) 
        overflow = true; 
      } 
     } 
    } 


    static SlidePart CloneSlidePart(PresentationPart presentationPart, SlidePart slideTemplate) 
    { 
     //Create a new slide part in the presentation 
     SlidePart newSlidePart = presentationPart.AddNewPart<SlidePart>("newSlide" + index); 
     index++; 
     //Add the slide template content into the new slide 
     newSlidePart.FeedData(slideTemplate.GetStream(FileMode.Open)); 
     //make sure the new slide references the proper slide layout 
     newSlidePart.AddPart(slideTemplate.SlideLayoutPart); 
     //Get the list of slide ids 
     SlideIdList slideIdList = presentationPart.Presentation.SlideIdList; 
     //Figure out where to add the next slide (find max slide) 
     uint maxSlideId = 1; 
     SlideId prevSlideId = null; 
     foreach (SlideId slideId in slideIdList.ChildElements) 
     { 
      if (slideId.Id > maxSlideId) 
      { 
       maxSlideId = slideId.Id; 
       prevSlideId = slideId; 
      } 
     } 
     maxSlideId++; 
     //Add new slide at the end of the deck 
     SlideId newSlideId = slideIdList.InsertAfter(new SlideId(), prevSlideId); 
     //Make sure id and relid is set appropriately 
     newSlideId.Id = maxSlideId; 
     newSlideId.RelationshipId = presentationPart.GetIdOfPart(newSlidePart); 
     return newSlidePart; 
    } 


    private static A.TableCell CreateTextCell(string text) 
    { 
     var textCol = new string[2]; 
     if (!string.IsNullOrEmpty(text)) 
     { 
      if (text.Length > 25) 
      { 
       textCol[0] = text.Substring(0, 25); 
       textCol[1] = text.Substring(26); 
      } 
      else 
      { 
       textCol[0] = text; 
      } 
     } 
     else 
     { 
      textCol[0] = string.Empty; 
     } 


     A.TableCell tableCell3 = new A.TableCell(); 

     A.TextBody textBody3 = new A.TextBody(); 
     A.BodyProperties bodyProperties3 = new A.BodyProperties(); 
     A.ListStyle listStyle3 = new A.ListStyle(); 

     textBody3.Append(bodyProperties3); 
     textBody3.Append(listStyle3); 


     var nonNull = textCol.Where(t => !string.IsNullOrEmpty(t)).ToList(); 

     foreach (var textVal in nonNull) 
     { 
      //if (!string.IsNullOrEmpty(textVal)) 
      //{ 
      A.Paragraph paragraph3 = new A.Paragraph(); 
      A.Run run2 = new A.Run(); 
      A.RunProperties runProperties2 = new A.RunProperties() { Language = "en-US", Dirty = false, SmartTagClean = false }; 
      A.Text text2 = new A.Text(); 
      text2.Text = textVal; 
      run2.Append(runProperties2); 
      run2.Append(text2); 
      paragraph3.Append(run2); 
      textBody3.Append(paragraph3); 
      //} 
     } 

     A.TableCellProperties tableCellProperties3 = new A.TableCellProperties(); 
     tableCell3.Append(textBody3); 
     tableCell3.Append(tableCellProperties3); 



     //var tc = new A.TableCell(
     //     new A.TextBody(
     //      new A.BodyProperties(), 
     //     new A.Paragraph(
     //      new A.Run(
     //       new A.Text(text)))), 
     //     new A.TableCellProperties()); 

     //return tc; 
     return tableCell3; 
    } 
} 
} 
相關問題