Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用OpenXMLSDKC向PPTX添加值?_C#_Sql_.net_Powerpoint_Openxml - Fatal编程技术网

C# 如何使用OpenXMLSDKC向PPTX添加值?

C# 如何使用OpenXMLSDKC向PPTX添加值?,c#,sql,.net,powerpoint,openxml,C#,Sql,.net,Powerpoint,Openxml,以下是我的要求: 我有一个PPTX模板。我使用OpenXMLSDK的反射代码选项来获取c代码。现在,我的要求是从SQLServer数据库向模板添加值。如果你能帮我解决这个问题就太好了 我被这件事弄得头破血流!如果没有SQL Server数据库插入,有人能告诉我如何编辑XML文档吗 一个非常简单的解决方案是,为可以替换的字符串制作自己的简单符号,如[CompanyName]。因为pptx文件只是zip文件,所以您可以在程序中打开它们,解析所有slide1.xml文件,并用所需的文本替换占位符。而不

以下是我的要求: 我有一个PPTX模板。我使用OpenXMLSDK的反射代码选项来获取c代码。现在,我的要求是从SQLServer数据库向模板添加值。如果你能帮我解决这个问题就太好了


我被这件事弄得头破血流!如果没有SQL Server数据库插入,有人能告诉我如何编辑XML文档吗

一个非常简单的解决方案是,为可以替换的字符串制作自己的简单符号,如
[CompanyName]
。因为pptx文件只是zip文件,所以您可以在程序中打开它们,解析所有slide1.xml文件,并用所需的文本替换占位符。而不是把所有的东西都存起来,再做一次拉链

好的,下面是一个逐步解决方案:

  • 使用或在内存中打开Zip文件
  • zip中有以下文件:
    ~ppt/slideLayouts
    ~ppt/slideMasters
    ~ppt/slides
    (幻灯片的编号如下:(slide1.xml、slide2.xml,…)
  • 打开文件夹中的每个文件,并用您的值替换标记。
    [CompanyName]-->Microsoft Inc.
  • 再次使用与
    .pptx
    相同的结构保存所有内容
  • 高兴
  • 您喜欢的methode的标题可能如下所示:

    /// <summary>
    /// Provide some power point helper methods
    /// </summary>
    public class PowerPointHelper
    {
        /// <summary>
        /// Prepare PPTX-File
        /// </summary>
        /// <param name="pptx">Byte-Array instance, containing the PPTX file</param>
        /// <param name="textToReplace">KeyValue Pairs which will be replaced in the PPTX</param>
        /// <returns>byte array containing the</returns>
        public byte[] PreparePPTX(byte[] pptx, IDictionary<string, string> textToReplace)
        {
            if (pptx == null)
            {
                throw new ArgumentNullException("pptx");
            }
    
            byte[] returnValue = pptx;
    
            if (textToReplace != null)
            {
                // ...
                // ...
                // ...
            }
    
            return returnValue;
        }
    }
    
    //
    ///提供一些power point辅助方法
    /// 
    公共类PowerPointHelper
    {
    /// 
    ///准备PPTX文件
    /// 
    ///字节数组实例,包含PPTX文件
    ///将在PPTX中替换的键值对
    ///包含
    公共字节[]PreparePPTX(字节[]pptx,IDictionary textToReplace)
    {
    如果(pptx==null)
    {
    抛出新的ArgumentNullException(“pptx”);
    }
    字节[]返回值=pptx;
    if(textToReplace!=null)
    {
    // ...
    // ...
    // ...
    }
    返回值;
    }
    }
    
    解决方案1

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.IO;
    using System.IO.Packaging;
    using System.Xml.Linq;
    using System.Linq;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Presentation;
    using DocumentFormat.OpenXml.Drawing;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            public const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
            static void Main(string[] args)
            {
                IDictionary<string, string> toReplace = new Dictionary<string, string>();
                toReplace.Add("Sample2", "Sample - Text!");
                toReplace.Add("Sample3", "Sample 3 - Text!");
    
                using (PresentationDocument presentationDocument = PresentationDocument.Open("C:\\Users\\beggers\\Desktop\\Test.pptx", true))
                {
                    // Get the presentation part of the presentation document.
                    PresentationPart presentationPart = presentationDocument.PresentationPart;
    
                    // Verify that the presentation part and presentation exist.
                    if (presentationPart != null && presentationPart.Presentation != null)
                    {
                        // Get the Presentation object from the presentation part.
                        Presentation presentation = presentationPart.Presentation;
    
                        // Verify that the slide ID list exists.
                        if (presentation.SlideIdList != null)
                        {
                            int slideNo = 1;
    
                            foreach (var slideId in presentation.SlideIdList.Elements<SlideId>())
                            {
                                Console.WriteLine("Slide number: {0}", slideNo);
                                SlidePart slidePart = presentationPart.GetPartById(slideId.RelationshipId) as SlidePart;
    
                                ShapeTree tree = slidePart.Slide.CommonSlideData.ShapeTree;
                                foreach (DocumentFormat.OpenXml.Presentation.Shape shape in tree.Elements<DocumentFormat.OpenXml.Presentation.Shape>())
                                {
                                    // Run through all the paragraphs in the document
                                    foreach (Paragraph paragraph in shape.Descendants().OfType<Paragraph>())
                                    {
                                        foreach (DocumentFormat.OpenXml.Drawing.Run run in paragraph.Elements<Run>())
                                        {
                                            foreach (var kvp in toReplace)
                                            {
                                                if (run.Text.InnerText.Contains(kvp.Key))
                                                {
                                                    run.Text = new DocumentFormat.OpenXml.Drawing.Text(kvp.Value);
                                                }
                                            }
                                        }
                                    }
                                }
    
                                slideNo++;
                            }
                        }
                    }
                }
    
                Console.ReadLine();
            }
        }
    }
    
    使用系统;
    使用系统集合;
    使用System.Collections.Generic;
    使用系统数据;
    使用系统诊断;
    使用System.IO;
    使用System.IO.Packaging;
    使用System.Xml.Linq;
    使用System.Linq;
    使用DocumentFormat.OpenXml.Packaging;
    使用DocumentFormat.OpenXml.Presentation;
    使用DocumentFormat.OpenXml.Drawing;
    命名空间控制台应用程序2
    {
    班级计划
    {
    公共常量字符串documentRelationshipType=”http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
    静态void Main(字符串[]参数)
    {
    IDictionary toReplace=新字典();
    添加(“Sample2”,“Sample-Text!”);
    添加(“Sample3”,“Sample3-Text!”);
    使用(PresentationDocument PresentationDocument=PresentationDocument.Open(“C:\\Users\\beggers\\Desktop\\Test.pptx”,true))
    {
    //获取演示文稿文档的演示文稿部分。
    PresentationPart PresentationPart=presentationDocument.PresentationPart;
    //验证演示文稿部分和演示文稿是否存在。
    if(presentationPart!=null&&presentationPart.Presentation!=null)
    {
    //从演示文稿部分获取演示文稿对象。
    演示文稿=演示文稿部分。演示文稿;
    //验证幻灯片ID列表是否存在。
    如果(presentation.SlideIdList!=null)
    {
    int-slideNo=1;
    foreach(presentation.slidedlist.Elements()中的var slideId)
    {
    WriteLine(“幻灯片编号:{0}”,slideNo);
    SlidePart SlidePart=presentationPart.GetPartById(slideId.RelationshipId)作为SlidePart;
    ShapeTree tree=slidePart.Slide.CommonSlideData.ShapeTree;
    foreach(DocumentFormat.OpenXml.Presentation.Shape-in-tree.Elements())
    {
    //浏览文档中的所有段落
    foreach(类型()的shape.subjects()中的段落)
    {
    foreach(DocumentFormat.OpenXml.Drawing.Run在段落.Elements()中)
    {
    foreach(var kvp处于替代状态)
    {
    if(run.Text.InnerText.Contains(kvp.Key))
    {
    run.Text=newdocumentformat.OpenXml.Drawing.Text(kvp.Value);
    }
    }
    }
    }
    }
    slideNo++;
    }
    }
    }
    }
    Console.ReadLine();
    }
    }
    }
    

    解决方案1需要引用
    DocumentFormat.OpenXml

    嘿,你现在有空吗?我有疑问,我正在工作。非常感谢你的帮助!@ShashankSuresh你有什么问题?以下是我执行的操作项目。1.创建了一个PPTX,其中包含一张幻灯片和一个文本框。2.填充文本框使用一些随机值。3.使用下面的vb.net代码和LINQ,我可以在XML文件中搜索ID,然后用我选择的值更新文本框。4.这里的问题是当我尝试将其重新创建为c#时。你能帮我解决吗?@ShashankSuresh下面的意思是什么?我如何向你发送代码?你能帮我吗告诉我你的电子邮件ID或什么的?我想给你发一个机器人