前言

本文分享以c#程序代码为例,实现将html文件转换word文档的方法(附vb.net代码)。在实际转换场景中可参考本文的方法,转换前,请按照如下方法引用word api的dll文件到visual studio。安装时,可通过以下2种方法:

1.通过nuget安装dll(2种方法)

1.1 可以在visual studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理nuget包”,然后搜索“free spire.doc”,点击“安装”。等待程序安装完成。

1.2 将以下内容复制到pm控制台安装:

install-package freespire.doc -version 10.2

2.手动添加dll引用

可通过手动下载包到本地,然后解压,找到bin文件夹下的spire.doc.dll。然后在visual studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径bin文件夹下的dll文件添加引用至程序。

实现代码

完成引用后,编辑如下代码实现格式转换:

c#

using spire.doc;

namespace htmltoword
{
    class program
    {
        static void main(string[] args)
        {
            //创建 document 对象
            document document = new document();

            //加载html文件
            document.loadfromfile("test.html");

            //将html文件转为word并保存
            document.savetofile("htmltoword.docx", fileformat.docx2013);
            system.diagnostics.process.start("htmltoword.docx");
        }
    }
}

vb.net

imports spire.doc

namespace htmltoword
    class program
        private shared sub main(args as string())
            '创建 document 对象
            dim document as new document()

            '加载html文件
            document.loadfromfile("test.html")

            '将html文件转为word并保存
            document.savetofile("htmltoword.docx", fileformat.docx2013)
            system.diagnostics.process.start("htmltoword.docx")
        end sub
    end class
end namespace

转换效果:

到此这篇关于c#/vb.net实现将html转为word的示例详解的文章就介绍到这了,更多相关c# html转word内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

发表评论

后才能评论