[.NET] 開頭不講"Hello Word",讀盡詩書也枉然 : Word 操作組件介紹 - Spire.Doc


開頭不講"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。

 

目錄

 

介紹

   這是 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 的強大遠遠不止如此。你使用該組件時所遇到的困難,我們可以共同來探討哦。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM