【问题标题】:Add table to powerpoint slide using Open XML使用 Open XML 将表格添加到 powerpoint 幻灯片
【发布时间】:2015-10-27 15:44:55
【问题描述】:

我使用代码生成器在 powerpoint 中获得了表格的代码,但我无法将表格添加到现有的 powerpoint 文档中。

我尝试将另一个表格添加到预期的幻灯片并执行以下操作:

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

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

但这没有用。

我对这个问题没有想法。 我最初的目标是在我的演示文稿中添加一个具有动态列数和固定行数的表格。

【问题讨论】:

    标签: powerpoint openxml-sdk


    【解决方案1】:

    我知道这个问题发布已经很久了,但以防万一有人需要一个工作代码来在 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;
        }
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-07-26
      • 2022-10-02
      • 1970-01-01
      • 2017-08-11
      • 1970-01-01
      • 2015-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多