C#实现Word和ODT文档相互转换详解
作者:E-iceblue 时间:2022-12-17 21:36:47
ODT文档格式一种开放文档格式(OpenDocument Text)。通常,ODT格式的文件可以使用LibreOffice Writer、MS Word或其他一些文档编辑器来打开。我们在处理文档时,可通过格式转换的方式,将ODT转为其他格式,或者将其他格式转为ODT,来获取目标文档。本文,以C#及VB.NET代码展示ODT和Word文档之间相互转换的方法。
程序环境
本次测试时,在程序中引入Free Spire.Doc for .NET。可通过以下方法引用Spire.Doc.dll文件:
方法1
将 Free Spire.Doc for .NET 下载到本地,解压,安装。安装完成后,找到安装路径下BIN文件夹中的Spire.Doc.dll。然后在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径BIN文件夹下的dll文件添加引用至程序。
方法2
通过 NuGet 安装。可通过以下2种方法安装:
(1)可以在Visual Studio中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理NuGet包”,然后搜索“Free Spire.Doc”,点击“安装”。等待程序安装完成。
(2)将以下内容复制到PM控制台安装。
Install-Package FreeSpire.Doc -Version 10.2.0
格式转换
转换时,只需要操作三行代码来实现:
1.创建Document类的对象。
2.调用Document.LoadFromFile(string fileName)方法加载源文档。
3.通过Document.SaveToFile(string fileName, FileFormat fileFormat)方法保存为目标文件格式到指定路径。
1. Word转为ODT
C#
using Spire.Doc;
namespace WordtoODT
{
class Program
{
static void Main(string[] args)
{
//创建Document类的对象
Document document = new Document();
//加载Word文档
document.LoadFromFile("sample.docx");
//保存为ODT格式
document.SaveToFile("ToODT.odt", FileFormat.Odt);
}
}
}
vb.net
Imports Spire.Doc
Namespace WordtoODT
Class Program
Private Shared Sub Main(args As String())
'创建Document类的对象
Dim document As New Document()
'加载Word文档
document.LoadFromFile("sample.docx")
'保存为ODT格式
document.SaveToFile("ToODT.odt", FileFormat.Odt)
End Sub
End Class
End Namespace
2. ODT转为Word
C#
using Spire.Doc;
namespace ODTtoWord
{
class Program
{
static void Main(string[] args)
{
//创建Document类的对象
Document document = new Document();
//加载ODT文档
document.LoadFromFile("test.odt");
//保存为Word格式
document.SaveToFile("toWord.docx", FileFormat.Docx2013);
}
}
}
vb.net
Imports Spire.Doc
Namespace ODTtoWord
Class Program
Private Shared Sub Main(args As String())
'创建Document类的对象
Dim document As New Document()
'加载ODT文档
document.LoadFromFile("test.odt")
'保存为Word格式
document.SaveToFile("toWord.docx", FileFormat.Docx2013)
End Sub
End Class
End Namespace
注:测试代码中的文件路径为程序Debug路径,文件路径可自定义为其他路径。
来源:https://www.cnblogs.com/Yesi/p/16252626.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
hibernate-validator改进校验框架validator v0.4使用
C#七大经典排序算法系列(上)
![](https://img.aspxhome.com/file/2023/5/72455_0s.png)
一篇文章带你入门java变量与类型
![](https://img.aspxhome.com/file/2023/3/67643_0s.png)
C#探秘系列(二)——IsXXX 系列方法
![](https://img.aspxhome.com/file/2023/5/100095_0s.png)
为什么rest接口返回json建议采用下划线形式,不要用驼峰
SpringMVC RESTFul及REST架构风格介绍
![](https://img.aspxhome.com/file/2023/7/80917_0s.png)
Android定时器和倒计时实现淘宝秒杀功能
![](https://img.aspxhome.com/file/2023/3/137153_0s.png)
Java女装商城系统的实现流程
![](https://img.aspxhome.com/file/2023/5/81255_0s.png)
Android开发导入项目报错Ignoring InnerClasses attribute for an anonymous inner class的解决办法
![](https://img.aspxhome.com/file/2023/2/113302_0s.png)
Androd 勇闯高阶性能优化之布局优化篇
![](https://img.aspxhome.com/file/2023/8/87278_0s.png)
Mybatis与Ibatis的区别
一篇文章带你了解Java SpringMVC返回null
![](https://img.aspxhome.com/file/2023/8/129128_0s.png)
Android三种网络通讯方式及Android的网络通讯机制
![](https://img.aspxhome.com/file/2023/2/139472_0s.png)
Java算法之时间复杂度和空间复杂度的概念和计算
![](https://img.aspxhome.com/file/2023/9/79519_0s.png)
java泛型常用通配符实例解析
![](https://img.aspxhome.com/file/2023/2/67472_0s.jpg)
浅谈SpringBoot中的Bean初始化方法 @PostConstruct
![](https://img.aspxhome.com/file/2023/1/80251_0s.jpg)
Android结合xml实现帧动画
![](https://img.aspxhome.com/file/2023/4/110674_0s.png)
SpringBoot中定时任务@Scheduled注解的使用解读
![](https://img.aspxhome.com/file/2023/9/62679_0s.png)