目录1.加载本地Word2.以只读模式加载Word3.从流加载Word【程序环境】 windows 10 Visual Studio 2017 Word
【程序环境】
【引用程序集】
在编辑代码加载Word
文档前,需要引入spire.doc.dll
到VS
,可直接在VS
中通过Nuget
下载安装到程序。具体步骤如下图:
等待安装。完成安装后,如图效果:
方法1:通过Document(string fileName
)构造函数,从指定的现有文档初始化Document
类的新实例。
C#
using Spire.Doc;
namespace TEST
{
class Program
{
static void Main(string[] args)
{
Document document = new Document(@"E:\Work\Documents\test.docx");
}
}
}
方法2 : 初始化Document
类的对象,然后调用LoadFromFile(string fileName)
方法加载本地路径中的Word
文档。
C#
using Spire.Doc;
namespace TEST
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFile(@"E:\Work\Documents\test.docx");
}
}
}
初始化Document
类的对象,调用Document.LoadFromFileInReadMode(string strFileName, FileFORMat
) 方法以只读模式加载本地路径中的Word
。
C#
using Spire.Doc;
namespace TEST
{
class Program
{
static void Main(string[] args)
{
Document document = new Document();
document.LoadFromFileInReadMode(@"E:\Work\Documents\test.docx", FileFormat.Docx);
}
}
}
方法1 : 构造函数Document(Stream stream)
从指定的数据流初始化Document
类的新实例。
C#
using Spire.Doc;
using System.IO;
namespace TEST
{
class Program
{
static void Main(string[] args)
{
Stream stream = File.OpenRead(@"E:\Work\Documents\test.docx");
Document document = new Document(stream);
}
}
}
方法2:初始化Document
类的对象,调用Document.LoadFromStream(Stream stream, FileFormat fileFormat
) 方法从流加载Word
。
C#
using Spire.Doc;
using System.IO;
namespace TEST
{
class Program
{
static void Main(string[] args)
{
Stream stream = File.OpenRead(@"E:\Work\Documents\test.docx");
Document document = new Document();
document.LoadFromStream(stream, FileFormat.Docx);
}
}
}
到此这篇关于3种C# 加载Word的方法的文章就介绍到这了,更多相关C#
加载Word的方法内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: 3种C# 加载Word的方法
本文链接: https://www.lsjlt.com/news/135805.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0