详解一款开源免费的.NET文档操作组件DocX(.NET组

在目前的软件项目中,都会较多的使用到对文档的操作,用于记录和统计相关业务信息。由于系统自身提供了对文档的相关操作,所以在一定程度上极大的简化了软件使用者的工作量。

在.NET项目中如果用户提出了相关文档操作的需求,开发者较多的会使用到微软自行提供的插件,在一定程度上简化了开发人员的工作量,但是同时也给用户带来了一些困扰,例如需要安装庞大的office,在用户体验性就会降低很多,并且在国内,很多人都还是使用wps,这就导致一部分只安装了wps的使用者很是为难,在对Excel的操作方面,有一个NPOI组件。那么可能会有人问有没有什么办法让这些困扰得到解决,答案是肯定的,那就是今天需要介绍的“DocX”组件,接下来我们就来了解一下这个组件的功能和用法。

一.DocX组件概述:

DocX是一个.NET库,允许开发人员以简单直观的方式处理Word 2007/2010/2013文件。 DocX是快速,轻量级,最好的是它不需要安装Microsoft Word或Office。DocX组件不仅可以完成对文档的一般要求,例如创建文档,创建表格和文本,并且还可以创建图形报表。DocX使创建和操作文档成为一个简单的任务。

它不使用COM库,也不需要安装Microsoft Office。在使用DocX组件时,你需要安装为了使用DocX是.NET框架4.0和Visual Studio 2010或更高版本。

DocX的主要特点:

(1).在文档中插入,删除或替换文本。所有标准文本格式都可用。 字体{系列,大小,颜色},粗体,斜体,下划线,删除线,脚本{子,超级},突出显示。

(2).段落属性显示。方向LeftToRight或RightToLeft;缩进;比对。  

(3).DocX也支持:图片,超链接,表,页眉和页脚,自定义属性。

有关DocX组件的相关信息就介绍到这里,如果需要更加深入的了解相关信息,可以进入:https://docx.codeplex.com/

二.DocX相关类和方法解析:

本文将结合DocX的源码进行解析,使用.NET Reflector对DLL文件进行反编译,以此查看源代码。将DLL文件加入.NET Reflector中,点击打开文件。 

 1.DocX.Create():创建文档。

public static DocX Create(Stream stream) { MemoryStream stream2 = new MemoryStream(); PostCreation(ref Package.Open(stream2, FileMode.Create, FileAccess.ReadWrite)); DocX cx = Load(stream2); cx.stream = stream; return cx; }

 2.Paragraph.Append:向段落添加信息。

public Paragraph Append(string text) { List<XElement> content = HelperFunctions.FormatInput(text, null); base.Xml.Add(content); this.runs = base.Xml.Elements(XName.Get("r", DocX.w.NamespaceName)).Reverse<XElement>().Take<XElement>(content.Count<XElement>()).ToList<XElement>(); return this; }

public Paragraph Bold() { this.ApplyTextFormattingProperty(XName.Get("b", DocX.w.NamespaceName), string.Empty, null); return this; }

3.Table.InsertTableAfterSelf:将数据插入表格。

public override Table InsertTableAfterSelf(int rowCount, int coloumnCount) { return base.InsertTableAfterSelf(rowCount, coloumnCount); } public virtual Table InsertTableAfterSelf(int rowCount, int coloumnCount) { XElement content = HelperFunctions.CreateTable(rowCount, coloumnCount); base.Xml.AddAfterSelf(content); return new Table(base.Document, base.Xml.ElementsAfterSelf().First<XElement>()); }

4.CustomProperty:自定义属性。

public class CustomProperty { // Fields private string name; private string type; private object value; // Methods public CustomProperty(string name, bool value); public CustomProperty(string name, DateTime value); public CustomProperty(string name, double value); public CustomProperty(string name, int value); public CustomProperty(string name, string value); private CustomProperty(string name, string type, object value); internal CustomProperty(string name, string type, string value); // Properties public string Name { get; } internal string Type { get; } public object Value { get; } }

5.BarChart:创建棒形图。

public class BarChart : Chart { // Methods public BarChart(); protected override XElement CreateChartXml(); // Properties public BarDirection BarDirection { get; set; } public BarGrouping BarGrouping { get; set; } public int GapWidth { get; set; } }

public abstract class Chart { // Methods public Chart(); public void AddLegend(); public void AddLegend(ChartLegendPosition position, bool overlay); public void AddSeries(Series series); protected abstract XElement CreateChartXml(); public void RemoveLegend(); // Properties public CategoryAxis CategoryAxis { get; private set; } protected XElement ChartRootXml { get; private set; } protected XElement ChartXml { get; private set; } public DisplayBlanksAs DisplayBlanksAs { get; set; } public virtual bool IsAxisExist { get; } public ChartLegend Legend { get; private set; } public virtual short MaxSeriesCount { get; } public List<Series> Series { get; } public ValueAxis ValueAxis { get; private set; } public bool View3D { get; set; } public XDocument Xml { get; private set; } }

6.Chart的AddLegend(),AddSeries(),RemoveLegend()方法解析:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjywys.html