![](/img/trans.png)
[英]how to add transition for slide to powerpoint using interop or open xml sdk c#
[英]How to add values to a PPTX using open XML SDK C#?
这是我的要求:我有一个PPTX模板。 我使用OPEN XML SDK的反射代码选项来获取c#代码。 现在,我的要求是从SQL Server数据库向模板添加值。 如果您能帮助我解决这个问题,那就太好了吗?
我为此感到震惊! 如果没有插入SQL Server数据库,有人可以告诉我如何编辑XML文档吗?
一个非常简单的解决方案是,为您可以替换的字符串制作自己的简单符号,例如[CompanyName]
。 由于pptx文件只是zip文件,因此您可以在程序中打开它们并解析所有slide1.xml文件,并用所需的文本替换占位符。 比保存所有东西然后再次拉上拉链。
好的,这是一个分步解决方案:
~ppt/slideLayouts
, ~ppt/slideLayouts
~ppt/slideMasters
和~ppt/slideMasters
~ppt/slides
(幻灯片的编号如下:(slide1.xml,slide2.xml等) [CompanyName] --> Microsoft Inc.
.pptx
相同的结构保存所有内容。 尝试执行的方法的标头可能类似于:
/// <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;
}
}
解决方案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();
}
}
}
解决方案1需要引用DocumentFormat.OpenXml
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.