使用C#保存PowerPoint文件

6
我只需要用C#创建一个带有1个虚拟幻灯片的.pptx文件,并将其保存到当前目录中。 有人能告诉我如何做吗?
到目前为止,我有以下代码来创建一个Powerpoint演示文稿:
Microsoft.Office.Interop.PowerPoint.Application obj = new Application();
obj.Visible = Microsoft.Office.Core.MsoTriState.msoTrue;
3个回答

2
这段代码片段创建一个新的演示文稿:
    private void DpStartPowerPoint()
{
    // Create the reference variables
    PowerPoint.Application ppApplication = null;
    PowerPoint.Presentations ppPresentations = null;
    PowerPoint.Presentation ppPresentation = null;

    // Instantiate the PowerPoint application
    ppApplication = new PowerPoint.Application();

    // Create a presentation collection holder
    ppPresentations = ppApplication.Presentations;

    // Create an actual (blank) presentation
    ppPresentation = ppPresentations.Add(Office.MsoTriState.msoTrue);

    // Activate the PowerPoint application
    ppApplication.Activate();
}

以下代码段可以保存它:

    // Assign a filename under which to save the presentation
string myFileName = "myPresentation";

// Save the presentation unconditionally
ppPresentation.Save();

// Save the presentation as a PPTX
ppPresentation.SaveAs(myFileName,
                      PowerPoint.PpSaveAsFileType.ppSaveAsDefault, 
                      Office.MsoTriState.msoTrue);

// Save the presentation as a PDF
ppPresentation.SaveAs(myFileName,
                      PowerPoint.PpSaveAsFileType.ppSaveAsPDF, 
                      Office.MsoTriState.msoTrue);

// Save a copy of the presentation
ppPresentation.SaveCopyAs(“Copy of “ + myFileName,
                          PowerPoint.PpSaveAsFileType.ppSaveAsDefault, 
                          Office.MsoTriState.msoTrue);

请参考此页面,了解其他幻灯片自动化功能。


1

你好,除了使用 Microsoft.Office.Interop.PowerPoint.Application obj = new Application(); 这种方式外,还有其他的方法吗?因为这种方式对我来说会抛出异常。 - user1221572
抛出了什么异常? - Dulini Atapattu
这个需要在你的电脑上安装PowerPoint才能运行吗? - user1221572
[System.Runtime.InteropServices.COMException] = {"检索具有 CLSID {91493441-5A91-11CF-8700-00AA0060263B} 的组件的 COM 类工厂失败,原因是以下错误: 80080005 服务器执行失败 (HRESULT 异常: 0x80080005 (CO_E_SERVER_EXEC_FAILURE))."} - user1221572

0
使用PowerPoint创建这样一个文件,并将其嵌入到您的C#应用程序中作为资源。然后在需要时将其复制到文件中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接