本文分享以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
复制代码 转换效果:

—END—
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |