您的位置:首页 > 其它

利用Spire for .NET实现办公自动化——Spire.Doc

2015-12-23 17:20 459 查看
今天研究了一下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组件进行完成。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: