星空网 > 软件开发 > ASP.net

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

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

【博主】反骨仔  【原文地址】http://www.cnblogs.com/liqingwen/p/5898368.html

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

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

 

目录

  • NuGet 包安装 Dll 引用文件
  • 开头不讲“Hello World”,读尽诗书也枉然
  • 文档内容检索
  • 文档内容替换
  • 设置字体
  • 缩进排版
  • 小结

 

介绍

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

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

图1

 

一、NuGet 包安装 Dll 引用文件

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

图1-1 打开 NuGet 包管理器

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

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

 

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

  1.先创建个空白的“demo1.docx”文件
[.NET] 开头不讲Hello Word,读尽诗书也枉然 : Word 操作组件介绍

图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   }

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

图 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     }

 

三、文档内容检索

  先在“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     }

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

图 3-1

 

 四、文档内容替换

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

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

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

图4-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 paragraph11         = 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     

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

 图5-1

 

六、缩进排版

  取空白的 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 paragraph11         = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();12 13       //Append Text14       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         else29         {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     }

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

 图6-1

小结

  以上只是几个小小的 Demo,当然,Spire.Doc 的强大远远不止如此。

 




原标题:[.NET] 开头不讲Hello Word,读尽诗书也枉然 : Word 操作组件介绍

关键词:.NET

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流