rjz*_*zii 5

另一个未被提及的选项,即我们最终采用的路径,是利用Office 2007和Office XP中通过兼容包支持的Open XML文件格式.使用Open XML SDK 1.0获得的工作结果令人惊讶地直截了当.

首先,准备了一个通用模板文件,其中代币代替了需要替换的内容.接下来,需要将对DocumentFormat.OpenXml的引用添加到项目中.代码本身将引用DocumentFormat.OpenXml和DocumentFormat.OpenXml.Packaging命名空间.最后,循环幻灯片的代码如下所示:

// Open the presentation
PresentationDocument presentation = PresentationDocument.Open(fileName, true);
// Loop through all of the slides in the presentation
foreach (SlidePart slide in presentation.PresentationPart.SlideParts)
{
    // Read the XML out of the slide
    XmlDocument xml = new XmlDocument();
    xml.Load(slide.GetStream());

    // TODO: Your XML manipulation code here

    // Save the updated slide
    xml.Save(slide.GetStream());
}
// Save the updated presentation
presentation.Close();
Run Code Online (Sandbox Code Playgroud)