Carina-baby

如何通过C#/VB.NET代码将PowerPoint转换为HTML

利用PowerPoint可以很方便的呈现多媒体信息,且信息形式多媒体化,表现力强。但难免在某些情况下我们会需要将PowerPoint转换为HTML格式。因为HTML文档能独立于各种操作系统平台(如Unix,Windows等)。并且它可以加入图片、声音、动画、影视等内容,还能从一个文件跳转到另一个文件,与世界各地主机的文件连接。通过HTML可以表现出丰富多彩的设计风格,实现页面之间的跳转,展现多媒体的效果。本文就将详细介绍如何通过C#/VB.NET代码将PowerPoint转换为HTML。

  • 将PowerPoint演示文稿转换为HTML
  • 将特定的PowerPoint幻灯片转换为HTML

程序环境

本次测试时,在程序中引入Free Spire.Presentation for .NET。可通过以下方法引用 Free Spire.Presentation.dll文件:

方法1:将 Free Spire.Presentation for .NET下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的 Spire.Presentation.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。

方法2:通过NuGet安装。可通过以下2种方法安装:

(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Presentation”,点击“安装”。等待程序安装完成。

(2)将以下内容复制到PM控制台安装。

Install-Package FreeSpire.Presentation -Version 7.8.0

将PowerPoint演示文稿转换为HTML

Presentation.SaveToFile(String, FileFormat) 方法用于将PowerPoint演示文稿转换为其他文件格式,如PDF、XPS和HTML。在以下步骤中,我们将向您展示如何使用Free Spire.Presentation for .NET将PowerPoint演示文稿转换为HTML:

  • 初始化Presentation类的实例。
  • 使用Presentation.LoadFromFile(String) 方法加载PowerPoint演示文稿。
  • 使用Presentation.SaveToFile(String, FileFormat) 方法将PowerPoint演示文稿保存为HTML格式。

完整代码

C#

using Spire.Presentation;
using System;

namespace ConvertPowerPointToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Presentation类的实例
            Presentation ppt = new Presentation();
            //加载PowerPoint演示文稿
            ppt.LoadFromFile("柯基.pptx");

            //指定输出HTML文件的文件路径
            String result = " D:\\.NET\\PowerPoint\\PowerPointToHtml.html";

            //将PowerPoint演示文稿保存为HTML格式
            ppt.SaveToFile(result, FileFormat.Html);
        }
    }
}

VB.NET

Imports Spire.Presentation

Namespace ConvertPowerPointToHtml
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '初始化Presentation类的实例
            Dim ppt As Presentation = New Presentation()
            '加载PowerPoint演示文稿
            ppt.LoadFromFile("柯基.pptx")

            '指定输出HTML文件的文件路径
            Dim result = " D:\\.NET\\PowerPoint\\PowerPointToHtml.html"

            '将PowerPoint演示文稿保存为HTML格式
            ppt.SaveToFile(result, FileFormat.Html)
        End Sub
    End Class
End Namespace

效果图

将特定的PowerPoint幻灯片转换为HTML

在某些情况下,您可能需要将特定的幻灯片而不是整个演示文稿转换为HTML。ISlide.SaveToFile(String, FileFormat) 方法可以将PowerPoint幻灯片转换为HTML。具体步骤如下:

  • 初始化Presentation类的实例。
  • 使用Presentation.LoadFromFile() 方法加载PowerPoint演示文稿。
  • 通过Presentation.Slides[int] 属性按索引获取PowerPoint演示文稿中的特定幻灯片。
  • 使用ISlide.SaveToFile(String, FileFormat) 方法将PowerPoint幻灯片保存为HTML格式。

完整代码

C#

using Spire.Presentation;
using System;

namespace ConvertPowerPointSlideToHtml
{
    class Program
    {
        static void Main(string[] args)
        {
            //初始化Presentation类的实例
            Presentation presentation = new Presentation();
            //加载PowerPoint演示文稿
            presentation.LoadFromFile("柯基.pptx");

            //获取特定幻灯片
            ISlide slide = presentation.Slides[5];

            //指定输出HTML文件的文件路径
            String result = " D:\\.NET\\PowerPoint\\SlideToHtml.html ";

            //将第一张幻灯片保存为HTML格式
            slide.SaveToFile(result, FileFormat.Html);
        }
    }
}

VB.NET

Imports Spire.Presentation

Namespace ConvertPowerPointSlideToHtml
    Friend Class Program
        Private Shared Sub Main(ByVal args As String())
            '初始化Presentation类的实例
            Dim presentation As Presentation = New Presentation()
            '加载PowerPoint演示文稿
            presentation.LoadFromFile("柯基.pptx")

            '获取特定幻灯片
            Dim slide As ISlide = presentation.Slides(5)

            '指定输出HTML文件的文件路径
            Dim result = " D:\.NET\PowerPoint\SlideToHtml.html "

            '将第一张幻灯片保存为HTML格式
            slide.SaveToFile(result, FileFormat.Html)
        End Sub
    End Class
End Namespace

效果图

—本文完—

 

相关文章: