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

简介: 今天研究了一下E-ICEBLUE公司的Spire for .NET系列产品。我们可以通过利用这个系列的dll库文件轻松的实现办公自动化的需求,而且不需要安装相应的办公软件。有关于Spire .NET系列产品的介绍戳这里可以看到。

今天研究了一下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组件进行完成。

相关文章
|
1月前
|
存储 BI 数据处理
Python自动化 | 解锁高效办公利器,Python助您轻松驾驭Excel!
Python自动化 | 解锁高效办公利器,Python助您轻松驾驭Excel!
|
1月前
|
测试技术 API 数据处理
Python办公自动化:解锁高效工作流程,掌握文档处理的艺术
Python办公自动化:解锁高效工作流程,掌握文档处理的艺术
83 1
|
2月前
|
数据采集 数据可视化 数据处理
【办公自动化】在Excel中按条件筛选数据并存入新的表2.0
【办公自动化】在Excel中按条件筛选数据并存入新的表2.0
48 1
|
2月前
|
数据可视化 数据挖掘 开发工具
【办公自动化】用Python批量从上市公司年报中获取主要业务信息
【办公自动化】用Python批量从上市公司年报中获取主要业务信息
62 0
|
2月前
|
Web App开发 人工智能 自然语言处理
【人工智能时代】AI赋能编程 | 自动化工具助力高效办公
【人工智能时代】AI赋能编程 | 自动化工具助力高效办公
【人工智能时代】AI赋能编程 | 自动化工具助力高效办公
|
2月前
|
数据采集 机器学习/深度学习 数据可视化
【办公自动化】使用Python批量处理Excel文件并转为csv文件
【办公自动化】使用Python批量处理Excel文件并转为csv文件
50 0
|
1月前
|
SQL 开发框架 .NET
OA办公自动化系统设计与实现(论文+源码)_kaic
OA办公自动化系统设计与实现(论文+源码)_kaic
|
2月前
|
存储 自然语言处理 数据可视化
【办公自动化】用Python按时间分割txt文件中的数据
【办公自动化】用Python按时间分割txt文件中的数据
55 1
|
2月前
|
编解码 数据可视化 数据挖掘
【办公自动化】用Python将PDF文件转存为图片
【办公自动化】用Python将PDF文件转存为图片
64 1
|
23天前
|
人工智能 数据可视化 机器人
【办公自动化】Excel透视表的简单应用
【办公自动化】Excel透视表的简单应用

热门文章

最新文章