【原文地址】http://www.cnblogs.com/liqingwen/p/5898368.html

  本打算过几天简单介绍下组件 Spire.XLS,突然发现园友率先发布了一篇,既然 xls 已经出现,为避免打上抄袭嫌疑,博主只能抢先一步使用 Spire.Doc 简单介绍 Doc 操作,下面是通过 WinForm 程序执行代码完成介绍的。

  本机环境:Win10 x64、VS 2015、MS Office 2016。

目录

介绍

  这是 E-iceblue 公司开发的其中一个组件 Spire.Doc,它专门为开发人员进行创建,读取,写入、转换打印 word 文档文件提供便利,并且,它不需要你安装 MS Office,就可以对 word 进行操作。这里使用的是免费版进行演示。

图1 官方截图

图2 版本间的功能的差异

一、NuGet 包安装 Dll 引用文件

图1-1 打开 NuGet 包管理器

图1-2 安装完后会多 3 个引用文件

二、开头不讲“Hello World”,读尽诗书也枉然

  1.先创建个空白的“demo1.docx”文件

图2-1

  2.随便写几句代码

 1     public partial class Form1 : Form
2 {
3 public Form1()
4 {
5 InitializeComponent();
6 }
7
8 private void button1_Click(object sender, EventArgs e)
9 {
10 //打开 word 文档
11 var document = new Document(@"demo1.docx",FileFormat.Docx);
12
13 //取第一部分
14 var section = document.Sections[0];
15
16 //取第一个段落
17 var paragraph = section.Paragraphs[0];
18
19 //追加字符串
20 paragraph.AppendText("Hello World!");
21
22 //保存为 .docx 文件
23 const string fileName = @"demo1-1.docx";
24 document.SaveToFile(fileName, FileFormat.Docx);
25
26 //启动该文件
27 Process.Start(fileName);
28 }
29 }

图 2-2 效果图

  【备注】别忘了引入命名空间哦: using Spire.Doc;

  上面是向一个空的 word 文档加上“Hello World!”,这次换成直接创建一个新的包含“Hello World!”内容的文档。当然效果跟图 2-2 一样。

 1         private void button1_Click(object sender, EventArgs e)
2 {
3 //创建 word 文档
4 var document = new Document();
5
6 //创建新的部分
7 var section = document.AddSection();
8
9 //创建新的段落
10 var paragraph = section.AddParagraph();
11
12 //追加字符串
13 paragraph.AppendText("Hello World!");
14
15 //保存为 .doc 文件
16 const string fileName = @"demo1-1.doc";
17 document.SaveToFile(fileName, FileFormat.Doc);
18
19 //启动该文件
20 Process.Start(fileName);
21 }

三、文档内容检索与替换

  1.内容检索

  先在“demo2.docx”中搞了篇《琵琶行》,启动时在文本框中输入“此时无声胜有声”进行检索。

 1         private void button1_Click(object sender, EventArgs e)
2 {
3 //加载 demo2.docx
4 var document = new Document(@"demo2.docx", FileFormat.Docx);
5
6 //查找所有匹配的字符串
7 TextSelection[] textSelections = document.FindAllString(this.textBox1.Text, false, false);
8
9 //修改背景色
10 foreach (TextSelection selection in textSelections)
11 {
12 selection.GetAsOneRange().CharacterFormat.TextBackgroundColor = Color.Gray;
13 }
14
15 //保存文件
16 const string fileName = @"demo2-1.docx";
17 document.SaveToFile(fileName, FileFormat.Docx);
18
19 //启动该文件
20 Process.Start(fileName);
21 }

图 3.1-1

  2.内容替换

  大家尝试在三的基础上简单修改下代码即可。

1             document.Replace(this.textBox1.Text, this.textBox2.Text,false,false);

图3.2-1

四、格式化操作 - 字体、颜色、排版缩进和样式等

  1.字体和颜色

  新建一个空白的 demo3.docx 文件。

 1         private void button1_Click(object sender, EventArgs e)
2 {
3 //加载 docx
4 var document = new Document(@"demo3.docx", FileFormat.Docx);
5
6 //获取第一个部分
7 Section section = document.Sections[0];
8
9 //创建一个新的段落或者取第一个段落
10 Paragraph paragraph
11 = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
12
13 //追加文本
14 const string text = "This paragraph is demo of text font and color. "
15 + "The font name of this paragraph is Tahoma. "
16 + "The font size of this paragraph is 20. "
17 + "The under line style of this paragraph is DotDot. "
18 + "The color of this paragraph is Blue. ";
19 TextRange txtRange = paragraph.AppendText(text);
20
21 //设置字体
22 txtRange.CharacterFormat.FontName = "Tahoma";
23
24 //设置字体大小
25 txtRange.CharacterFormat.FontSize = 20;
26
27 //设置下划线
28 txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;
29
30 //改变字体颜色
31 txtRange.CharacterFormat.TextColor = Color.Blue;
32
33 //保存文件
34 const string fileName = @"demo3-1.docx";
35 document.SaveToFile(fileName, FileFormat.Docx);
36
37 //启动该文件
38 Process.Start(fileName);
39

图4.1-1

  2.排版缩进

  取空白的 docx 文件。

 1         private void button1_Click(object sender, EventArgs e)
2 {
3 //加载 docx
4 var document = new Document(@"demo3.docx", FileFormat.Docx);
5
6 //获取第一个部分
7 Section section = document.Sections[0];
8
9 //创建一个新的段落或者取第一个段落
10 Paragraph paragraph
11 = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
12
13 //Append Text
14 paragraph.AppendText("这是缩进排版 Demo。");
15 paragraph.ApplyStyle(BuiltinStyle.Heading3);
16
17 var random = new Random();
18 paragraph = section.AddParagraph();
19 for (var i = 0; i < random.Next(0, 10); i++)
20 {
21 paragraph = section.AddParagraph();
22 paragraph.AppendText($"I'm {i}");
23
24 if (i == 0)
25 {
26 paragraph.ListFormat.ApplyBulletStyle();
27 }
28 else
29 {
30 paragraph.ListFormat.ContinueListNumbering();
31 }
32
33 paragraph.ListFormat.CurrentListLevel.NumberPosition = -10;
34 }
35
36 //保存文件
37 const string fileName = @"缩进排版.docx";
38 document.SaveToFile(fileName, FileFormat.Docx);
39
40 //启动该文件
41 Process.Start(fileName);
42 }

图4.2-1

  3.文本样式

 1         private void button1_Click(object sender, EventArgs e)
2 {
3 //创建一个新的 word
4 var document = new Document();
5
6 //创建第一部分
7 var section = document.AddSection();
8
9 //创建第一个段落
10 var paragraph = section.AddParagraph();
11
12 //追加字符串
13 paragraph.AppendText("Builtin Style:");
14
15 foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
16 {
17 paragraph = section.AddParagraph(); //增加段落
18
19 paragraph.AppendText(builtinStyle.ToString()); //追加文本
20
21 paragraph.ApplyStyle(builtinStyle); //应用样式
22 }
23
24 const string fileName = "Style.docx";
25 document.SaveToFile(fileName, FileFormat.Docx); //保存文件
26
27 Process.Start(fileName); //启动
28 }

图4.3-1

小结

  以上只是几个小小的 Demo,当然,Spire.Doc 的强大远远不止如此。你使用该组件时所遇到的困难,我们可以共同来探讨哦。

开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc (转)的更多相关文章

  1. [.NET] 开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc

    开头不讲"Hello Word",读尽诗书也枉然 : Word 操作组件介绍 - Spire.Doc [博主]反骨仔 [原文地址]http://www.cnblogs.com/li ...

  2. Word 操作组件介绍 - Spire.Doc

    http://www.cnblogs.com/liqingwen/p/5898368.html

  3. 【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式

    背景: 年11月,微软宣布作为ECMA国际主要合作伙伴,将其开发的基于XML的文件格式标准化,称之为"Office Open XML" .Open XML的引进使office文档结 ...

  4. 读Zepto源码之样式操作

    这篇依然是跟 dom 相关的方法,侧重点是操作样式的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码版本 本文阅读的源码为 zepto1.2. ...

  5. 读Zepto源码之属性操作

    这篇依然是跟 dom 相关的方法,侧重点是操作属性的方法. 读Zepto源码系列文章已经放到了github上,欢迎star: reading-zepto 源码版本 本文阅读的源码为 zepto1.2. ...

  6. 在C#中使用Spire.doc对word的操作总结

    在C#中使用Spire.doc对word的操作总结 在最近的工程中我们要处理一些word文档.通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复.极少数可以完全实现的word组件又要收费. ...

  7. 使用FreePic2Pdf导出书签至Word建立层级目录——快速初始化Word笔记本目录

    使用FreePic2Pdf导出书签至Word建立层级目录 --快速初始化Word笔记本目录 文:安徽师范大学2014级计算机科学与技术 王昊 (Get Contact:441301158@qq.com ...

  8. Spire.Doc组件读取与写入Word

    之前写了一篇开源组件DocX读写word的文章,当时时间比较匆忙选了这个组件,使用过程中还是有些不便,不能提前定义好模版,插入Form表单域进行替换.最近无意中发现Spire.Doc组件功能很强大,目 ...

  9. Word转图片(使用Spire.doc)

    Spire.Doc for .NET是一款由E-iceblue公司开发的专业的Word .NET类库.支持.net,WPF,Silverlight, 下载地址:http://www.e-iceblue ...

随机推荐

  1. HTML5网页点击分享到whatsapp

    一.在网页头部加入分享标题和url,代码如下: <meta name="whatsapp:url" class="share_url" content=& ...

  2. scala的多种集合的使用(3)之遍历集合的方法

    遍历集合的方法 1.用foreach循环遍历一个集合 foreach接收一个函数作为参数.定义的函数应该接收一个元素作为输入参数,然后不要返回任何的东西.输入的参数的类型应该匹配集合中的类型.随着fo ...

  3. PHP知识点记录

    笔记源自---PHP必须知道的300个问题 一.提高编程效率 1.echo中字符串用单引号包含起来会比用双引号快. 2.$row['id']的速度要比$row[id]快很多. 3.echo比print ...

  4. codeforces342B

    Xenia and Spies CodeForces - 342B Xenia the vigorous detective faced n (n ≥ 2) foreign spies lined u ...

  5. package---包

    一,包 包(package) 用于管理程序中的类,主要用于解决类的同名问题.包可以看成目录. 包的作用: [1] 防止命名冲突. [2] 允许类组成一个单元(模块),便于管理和维护 [3] 更好的保护 ...

  6. Git——开启区分大小写

    前言 默认情况下git是忽略区分大小写的,多人合作的情况下不规范很容易造成问题,所以开启区分大小写. 步骤 开启 全局开启 git config --global core.ignorecase fa ...

  7. docker 镜像备份magento 2.2.3

    打包百度网盘: 链接:https://pan.baidu.com/s/1i_lSLt48RlxPonVA7qG5kg  密码:h1de 还原:docker load < (filename).t ...

  8. Luogu3768简单的数学题

    题目描述 题解 我们在一通化简上面的式子之后得到了这么个东西. 前面的可以除法分块做,后面的∑T2∑dµ(T/d)是积性函数,可以线性筛. 然后这个数据范围好像不太支持线性筛,所以考虑杜教筛. 后面那 ...

  9. MySql数据库字段排序规则不一致产生的一个问题

    最近项目向MySql迁移,迁移完毕后,在获取用户权限时产生了一个异常,跟踪进去获取执行的语句如下, SELECT PermissionId FROM spysxtPermission WHERE (R ...

  10. win10更改无线网卡的MAC地址

    https://blog.csdn.net/qq_31778495/article/details/80932472 前段时间电脑蹭网被禁了MAC地址,故寻找修改MAC地址的方法. 本机配置: win ...