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

来源:http://www.cnblogs.com/liqingwen/archive/2016/09/23/5898368.html
-Advertisement-
Play Games

開頭不講"Hello Word",讀盡詩書也枉然 : Word 操作組件介紹 - Spire.Doc 【博主】反骨仔 【原文地址】http://www.cnblogs.com/liqingwen/p/5898368.html 序 本打算過幾天簡單介紹下組件 Spire.XLS,突然發現園友率先發佈了 ...


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

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • c#自定義日誌記錄 很簡單:將類複製到項目中,最後在配置文件上配置一下:logUrl即可。 預設保存在:項目/temp/log ...
  • 當你在開發程式的時候, 調試(debugging)和日誌(logging)都是非常重要的工作。在應用中使用日誌主要有三個目的 l 監視代碼中的變數的變化情況,把數據周期性地記錄到文件中供其它應用進行統計分析工作 l 跟蹤代碼運行的軌跡,作為日後審計的依據 l 擔當集成開發環境中的調試器,... ...
  • 1、Connection對象主要提供與資料庫的連接功能 配置web.config文件 <appSettings> <add key="ConnectionString" value="Server=10.136.*.*;database=MTL;uid=sa;pwd=sa;"/> </appSett ...
  • // StringBuffer sb = new StringBuffer();// for(Object bid :list){// sb.append(bid+",");// }// return sb.deleteCharAt(sb.length()-1).toString(); ...
  • Introduction to ASP.NET Core Asp.net core 介紹 270 of 282 people found this helpful By Daniel Roth, Rick Anderson and Shaun Luttin Meng.Net 自譯 ASP.NET C ...
  • 中國式商業智能報表ActiveReports免費公開課,10月20日開講。適合人群:報表開發人員,報表產品經理,商業報表使用構建人員。 ...
  • 20幾歲,怕什麼。 自己的感覺 說一個自己最近使用AngularJS的感受,我們之前使用mvc進行項目開發都是瞭解和經常使用HTML的幫助類,來完成我們前端大部分代碼的編寫,其實在我沒有接觸AngularJS之前對於這種方法還是很喜歡的,畢竟它是將.aspx頁面革命掉的東西,但是隨著項目中使用... ...
  • 2016年9月22日凌晨,微信宣佈“小程式”問世,當然只是開始內測了,微信公眾平臺對200個服務號發送了小程式內測邀請。那麼什麼是“小程式”呢,來看微信之父怎麼說 看完之後,相信大家大概都有些明白了吧,對於開發者來說或許都有些小激動吧,畢竟多關註一些新東西沒什麼不好。那麼問題是“小程式”只有200個 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...