利用Spire for .NET實現辦公自動化——Spire.Doc


今天研究了一下E-ICEBLUE公司的Spire for .NET系列產品。我們可以通過利用這個系列的dll庫文件輕松的實現辦公自動化的需求,而且不需要安裝相應的辦公軟件。有關於Spire .NET系列產品的介紹戳這里可以看到。下面我以Spire.Doc這個dll庫為例,寫一下它的使用過程(我的虛擬機上沒有下載與安裝Windows Office之類的辦公軟件):

1、下載Spire.Doc.Dll文件(下載地址):

2、將上面五個文件copy到項目的debug路徑下:

2、這里我在VS中新建一個控制台類型的project並命名為SpireDocUse,右鍵項目->Add->Reference->Browse->選擇Spire.Doc.dll文件,完成引用:

3、在項目中using這個dll庫:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

4、接下來就可以參考官網上的教程來操作了,這里舉個例子。創建一個word文檔->寫入一些內容->設置一下樣式,然后保存:

using System;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System.Drawing;

namespace SpireDocUse
{
    class Program
    {
        static void Main(string[] args)
        {
            //Configure path.
            string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string filePath = desktopPath + @"\test.docx";
            string picPath = desktopPath + @"\wang.jpg";
            //Create a word document.
            Document doc = new Document();
            //Add a section.
            Section section = doc.AddSection();
            //Add a paragraph.
            Paragraph paragraph = section.AddParagraph();
            paragraph.AppendText("Spire is me.");
            //Add a comment.
            string content = "CNblog:http://www.cnblogs.com/LanTianYou/";
            Comment comment = paragraph.AppendComment(content);
            comment.Format.Author = "Tylan";
            //Set font style for the paragraph.
            ParagraphStyle style = new ParagraphStyle(doc);
            style.Name = "TylanFontStyle";
            style.CharacterFormat.FontName = "Batang";
            style.CharacterFormat.FontSize = 36;
            style.CharacterFormat.TextColor = Color.Green;
            doc.Styles.Add(style);
            paragraph.ApplyStyle(style.Name);
            //Insert a picture.
            DocPicture pic = paragraph.AppendPicture(Image.FromFile(picPath));
            pic.Width = 500;
            pic.Height = 500;
            //Add header.
            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;
            Paragraph headerParagraph = header.AddParagraph();
            TextRange headerText = headerParagraph.AppendText("Spire header");
            headerText.CharacterFormat.FontSize = 18;
            headerText.CharacterFormat.TextColor = Color.Tomato;
            headerParagraph.Format.Borders.Bottom.BorderType = BorderStyle.ThinThinSmallGap;
            headerParagraph.Format.Borders.Bottom.Space = 0.15f;
            headerParagraph.Format.Borders.Color = Color.DarkGray;
            //Add footer.
            HeaderFooter footer = doc.Sections[0].HeadersFooters.Footer;
            Paragraph footerParagraph = footer.AddParagraph();
            TextRange footerText = footerParagraph.AppendText("Spire footer");
            footerText.CharacterFormat.FontSize = 18;
            footerText.CharacterFormat.TextColor = Color.Tomato;
            footerParagraph.Format.Borders.Top.BorderType = BorderStyle.ThinThinSmallGap;
            footerParagraph.Format.Borders.Top.Space = 0.15f;
            footerParagraph.Format.Borders.Color = Color.DarkGray;
            //Save the file.
            doc.SaveToFile(filePath, FileFormat.Docx);
        }
    }
}

運行結果(在桌面生成一個word文檔):

在安有word的辦公機打開這個文件:

通過以上的例子,我們實現了在無Office的環境下實現辦公的需求。通過Spire.NET可以對word文檔實現一系列的操作。除了Spire.Doc庫還有很多的.NET組件我們都可以選擇使用,可以在官網首頁的.NET模塊中看到:

 

在日常的工作中,我們可以像上述過程一樣,對Spire.Doc庫中封裝好的API進行一次再封裝,以滿足我們的自動化需求。具體可以根據自己的需求來引用相應的Spire .NET組件進行完成。


免責聲明!

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



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