利用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库:
4、接下来就可以参考官网上的教程来操作了,这里举个例子。创建一个word文档->写入一些内容->设置一下样式,然后保存:
运行结果(在桌面生成一个word文档):
在安有word的办公机打开这个文件:
通过以上的例子,我们实现了在无Office的环境下实现办公的需求。通过Spire.NET可以对word文档实现一系列的操作。除了Spire.Doc库还有很多的.NET组件我们都可以选择使用,可以在官网首页的.NET模块中看到:
在日常的工作中,我们可以像上述过程一样,对Spire.Doc库中封装好的API进行一次再封装,以满足我们的自动化需求。具体可以根据自己的需求来引用相应的Spire .NET组件进行完成。
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组件进行完成。
相关文章推荐
- union--求集合的并集
- Demo制作
- eclipse导入不到嵌套的项目
- tomcat下两个网站,后台新闻更新之后,前台页面不展示,tomcat报错-连接池错误c3p0
- 中文unicode范围及unicode编解码
- 微信公众平台开发(76) 获取用户基本信息
- 调用system函数时输出重定向,导致输出顺序错乱
- salesforce中对象字段类型基础
- Android Studio常用快捷键
- PHP获取当前时间、时间戳的各种格式写法汇总
- setxor--求两个集合交集的非(异或)
- 如何用NSLog输出NSRange,CGRect等结构体
- OCR(光学字符识别)的简单解析与使用(内含 Demo)
- 批处理、分时、实时、网络、分布式操作系统的区别
- android渐变动画的两种实现
- spatialhadoop2.3源码阅读(十三) RTreeGridOutputFormat & RTreeGridRecordWriter & RTree[RTree Index MapReuce]
- (原)使用opencv的warpAffine函数对图像进行旋转
- Binary Tree Paths return all root-to-leaf paths.
- lua-function
- setdiff--求两个集合的差