最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中使用Spire.doc對(duì)word的操作方式

 更新時(shí)間:2023年01月25日 16:09:35   作者:Eiceblue  
這篇文章主要介紹了C#中使用Spire.doc對(duì)word的操作方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用Spire.doc對(duì)word的操作

在最近的工程中我們要處理一些word文檔。通過(guò)在網(wǎng)上的大量搜索,我發(fā)現(xiàn)大多數(shù)軟件功能不是不完整就是有重復(fù)。極少數(shù)可以完全實(shí)現(xiàn)的word組件又要收費(fèi)。

功夫不負(fù)有心人,我們找到了可以滿足我們需要的免費(fèi)的C# word程序庫(kù)。為了和其他的作比較,我在這里先做以下匯總。希望對(duì)大家有幫助。

如何得到?

這個(gè)免費(fèi)版的word 組件可以在Codeplex下載到,你也可以從本文里直接下載msi文件。它還提供了一些源代碼。

Word操作匯總

1、打開(kāi)已有word文件,這是要處理word文件的最基本也是必須的步驟。它提供了三種方法。

方法1:從已知特定的文檔中初始化一個(gè)新的Document 類(lèi)的實(shí)例

Document document = new Document(@"..\..\Sample.docx");

方法2、從文件中加載一個(gè)word文件

Document document = new Document();
document.LoadFromFile(@"..\..\Sample.docx");

方法3、從流文件中加載word文件

Stream stream = File.OpenRead(@"..\..\Sample.docx");
Document document = new Document(stream);

2、如何創(chuàng)建表格 

Document document = new Document();
Section section = document.AddSection();
 
Table table = section.AddTable(true);
table.ResetCells(2, 3);
 
TableRow row = table.Rows[0];
row.IsHeader = true;
 
Paragraph para = row.Cells[0].AddParagraph();
TextRange TR = para.AppendText("Item");
 
para = row.Cells[1].AddParagraph();
TR = para.AppendText("Description");
 
para = row.Cells[2].AddParagraph();
TR = para.AppendText("Qty");
 
document.SaveToFile("WordTable.docx");
 
System.Diagnostics.Process.Start("WordTable.docx");

我們還可以設(shè)置行高和列寬

3、如何插入超鏈接?你可以插入兩種超鏈接,Email 鏈接和webmail 鏈接。

Document document = new Document();
Section section = document.AddSection();
 
//Insert URL hyperlink
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Home page");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com",HyperlinkType.WebLink);
 
//Insert email address hyperlink
paragraph = section.AddParagraph();
paragraph.AppendText("Contact US");
paragraph.ApplyStyle(BuiltinStyle.Heading2);
paragraph = section.AddParagraph();
paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com",HyperlinkType.EMailLink);
 
document.SaveToFile("Hyperlink.docx");
System.Diagnostics.Process.Start("Hyperlink.docx");

4、如何加入注解

Document document = new Document();
Section section = document.AddSection();
 
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Home Page of ");
TextRange textRange = paragraph.AppendText("e-iceblue");
 
Comment comment1 = paragraph.AppendComment("www.e-iceblue.com");
comment1.AddItem(textRange);
comment1.Format.Author = "Harry Hu";
comment1.Format.Initial = "HH";
 
document.SaveToFile("Comment.docx");
System.Diagnostics.Process.Start("Comment.docx");

5、如何加入書(shū)簽

Document document = new Document();
Section section = document.AddSection();
 
Paragraph paragraph = section.AddParagraph();
paragraph.AppendText("Home Page of ");
TextRange textRange = paragraph.AppendText("e-iceblue");
 
Comment comment1 = paragraph.AppendComment("www.e-iceblue.com");
comment1.AddItem(textRange);
comment1.Format.Author = "Harry Hu";
comment1.Format.Initial = "HH";
 
document.SaveToFile("Comment.docx");
System.Diagnostics.Process.Start("Comment.docx");

6、合并郵件

Document document = new Document();
document.LoadFromFile("Fax.doc");
 
string[] filedNames = new string[] { "Contact Name", "Fax", "Date" };
 
string[] filedValues = new string[] { "John Smith", "+1 (69) 123456", System.DateTime.Now.Date.ToString() };
 
document.MailMerge.Execute(filedNames, filedValues);
 
document.SaveToFile("MailMerge.doc", FileFormat.Doc);
System.Diagnostics.Process.Start("MailMerge.doc");

7、加入表單,這部分包含創(chuàng)建以及填入表單域。

創(chuàng)建表單

//Add new section to document
Section section = document.AddSection();
 
//Add Form to section
private void AddForm(Section section)
 
//add text input field
TextFormField field
= fieldParagraph.AppendField(fieldId, FieldType.FieldFormTextInput) as TextFormField;
 
//add dropdown field
DropDownFormField list
= fieldParagraph.AppendField(fieldId, FieldType.FieldFormDropDown) as DropDownFormField;
 
//add checkbox field
fieldParagraph.AppendField(fieldId, FieldType.FieldFormCheckBox);

填入表單域

//Fill data from XML file
using (Stream stream = File.OpenRead(@"..\..\..\Data\User.xml"))
{
    XPathDocument xpathDoc = new XPathDocument(stream);
XPathNavigator user = xpathDoc.CreateNavigator().SelectSingleNode("/user");
 
Fill data:
 
foreach (FormField field in document.Sections[0].Body.FormFields)
  {
     String path = String.Format("{0}/text()", field.Name);
     XPathNavigator propertyNode = user.SelectSingleNode(path);
     if (propertyNode != null)
     {
         switch (field.Type)
         {
             case FieldType.FieldFormTextInput:
                  field.Text = propertyNode.Value;
                  break;
 
             case FieldType.FieldFormDropDown:
                  DropDownFormField combox = field as DropDownFormField;
                  for(int i = 0; i < combox.DropDownItems.Count; i++)
                  {
                      if (combox.DropDownItems[i].Text == propertyNode.Value)
                      {
                         combox.DropDownSelectedIndex = i;
                         break;
                      }
                      if (field.Name == "country" && combox.DropDownItems[i].Text =="Others")
                      {
                         combox.DropDownSelectedIndex = i;
                      }
                  }
                  break;
 
             case FieldType.FieldFormCheckBox:
                  if (Convert.ToBoolean(propertyNode.Value))
                  {
                      CheckBoxFormField checkBox = field as CheckBoxFormField;
                      checkBox.Checked = true;
                  }
                  break;
            }
       }
   }
 }

8、合并word文檔

//Load two documents
//Load Document1 and Document2
Document DocOne = new Document();
DocOne.LoadFromFile(@"E:\Work\Document\welcome.docx", FileFormat.Docx);
Document DocTwo = new Document();
DocTwo.LoadFromFile(@"E:\Work\Document\New Zealand.docx", FileFormat.Docx);
 
//Merge
foreach (Section sec in DocTwo.Sections)
{
 DocOne.Sections.Add(sec.Clone());
}
//Save and Launch
DocOne.SaveToFile("Merge.docx", FileFormat.Docx);

9、保護(hù)文檔。你可以設(shè)置密碼或者加入水印來(lái)進(jìn)行保護(hù)。文字和圖片的水印都支持。

//Protect with password
document.Encrypt("eiceblue");
 
//Add Text watermark:
TextWatermark txtWatermark = new TextWatermark();
txtWatermark.Text = "Microsoft";
txtWatermark.FontSize = 90;
txtWatermark.Layout = WatermarkLayout.Diagonal;
document.Watermark = txtWatermark;
 
//Add Image watermark:
PictureWatermark picture = new PictureWatermark();
picture.Picture = System.Drawing.Image.FromFile(@"..\imagess.jpeg");
picture.Scaling = 250;
document.Watermark = picture;

10、轉(zhuǎn)換功能是在處理word文檔時(shí)最常見(jiàn)的操作了。使用免費(fèi)版的Spire.doc  for .NET, 轉(zhuǎn)換變得很簡(jiǎn)單。只要包含三行類(lèi)似的代碼你就可以把word轉(zhuǎn)換成其他常用格式,像PDF,HTML和圖片。

Word轉(zhuǎn)換成PDF

document.SaveToFile("Target PDF.PDF", FileFormat.PDF);

Word轉(zhuǎn)換成圖片

Image image = document.SaveToImages(0, ImageType.Bitmap);
image.Save("Sample.tiff", ImageFormat.Tiff);

word轉(zhuǎn)換成HTML

document.SaveToFile("Target HTML.html", FileFormat.Html);
WordDocViewer(""Target HTML.html");

結(jié)論

這是一個(gè)免費(fèi)又強(qiáng)大的C# word 組件,它不需要 Word automatio即可運(yùn)行,并且任何第三方的功能都囊括。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Unity實(shí)現(xiàn)Flappy Bird游戲開(kāi)發(fā)實(shí)戰(zhàn)

    Unity實(shí)現(xiàn)Flappy Bird游戲開(kāi)發(fā)實(shí)戰(zhàn)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)Flappy Bird游戲開(kāi)發(fā)實(shí)戰(zhàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • 在C#中如何使用正式表達(dá)式獲取匹配所需數(shù)據(jù)

    在C#中如何使用正式表達(dá)式獲取匹配所需數(shù)據(jù)

    本文給大家分享C#中如何使用正式表達(dá)式獲取匹配所需數(shù)據(jù) ,非常實(shí)用,對(duì)正則表達(dá)式獲取匹配相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-03-03
  • C#隨機(jī)設(shè)置900-1100毫秒延遲的方法

    C#隨機(jī)設(shè)置900-1100毫秒延遲的方法

    這篇文章主要介紹了C#隨機(jī)設(shè)置900-1100毫秒延遲的方法,涉及C#中Thread.Sleep方法的使用技巧,需要的朋友可以參考下
    2015-04-04
  • 詳解WPF如何把checkbox改成開(kāi)關(guān)樣式

    詳解WPF如何把checkbox改成開(kāi)關(guān)樣式

    這篇文章主要為大家詳細(xì)介紹了WPF如何把checkbox改成開(kāi)關(guān)樣式,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考一下
    2024-11-11
  • 使用 C# 下載文件的多種方法小結(jié)

    使用 C# 下載文件的多種方法小結(jié)

    本文從最簡(jiǎn)單的下載方式開(kāi)始步步遞進(jìn),講述了文件下載過(guò)程中的常見(jiàn)問(wèn)題并給出了解決方案。并展示了如何使用多線程提升 HTTP 的下載速度以及調(diào)用 aria2 實(shí)現(xiàn)非 HTTP 協(xié)議的文件下載,對(duì)C# 下載文件相關(guān)知識(shí)感興趣的朋友一起看看吧
    2021-08-08
  • C#中數(shù)據(jù)類(lèi)型的轉(zhuǎn)換介紹

    C#中數(shù)據(jù)類(lèi)型的轉(zhuǎn)換介紹

    大家好,本篇文章主要講的是C#中數(shù)據(jù)類(lèi)型的轉(zhuǎn)換介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • C#將配置文件appsetting中的值轉(zhuǎn)換為動(dòng)態(tài)對(duì)象調(diào)用

    C#將配置文件appsetting中的值轉(zhuǎn)換為動(dòng)態(tài)對(duì)象調(diào)用

    這篇文章主要介紹了將配置文件appsetting中的值轉(zhuǎn)換為動(dòng)態(tài)對(duì)象調(diào)用 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • C# Console.WriteLine()用法案例詳解

    C# Console.WriteLine()用法案例詳解

    這篇文章主要介紹了C# Console.WriteLine()用法案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#調(diào)用WebService的方法介紹

    C#調(diào)用WebService的方法介紹

    這篇文章介紹了C#調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • Quartz.Net調(diào)度框架配置解析

    Quartz.Net調(diào)度框架配置解析

    這篇文章主要為大家詳細(xì)介紹了Quartz.Net調(diào)度框架的配置方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07

最新評(píng)論

博野县| 武汉市| 鄢陵县| 连山| 蓬莱市| 清徐县| 孝感市| 绥阳县| 尉犁县| 高阳县| 容城县| 望谟县| 蕲春县| 凉山| 布尔津县| 上杭县| 锡林浩特市| 台江县| 黄梅县| 朝阳区| 龙川县| 渑池县| 井研县| 安福县| 边坝县| 保靖县| 宿州市| 呼图壁县| 宜阳县| 锡林郭勒盟| 武山县| 宁乡县| 平阳县| 安龙县| 晋宁县| 余干县| 若尔盖县| 梁河县| 河津市| 南召县| 班戈县|