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

public void AddLegend(ChartLegendPosition position, bool overlay) { if (this.Legend != null) { this.RemoveLegend(); } this.Legend = new ChartLegend(position, overlay); this.ChartRootXml.Add(this.Legend.Xml); }

public void AddSeries(Series series) { if (this.ChartXml.Elements(XName.Get("ser", DocX.c.NamespaceName)).Count<XElement>() == this.MaxSeriesCount) { throw new InvalidOperationException("Maximum series for this chart is" + this.MaxSeriesCount.ToString() + "and have exceeded!"); } this.ChartXml.Add(series.Xml); }

public void RemoveLegend() { this.Legend.Xml.Remove(); this.Legend = null; }

以上是对DocX组件的一些方法的一些简单解析,如果需要知道更多的方法实现代码,可自行进行下载查看。

三.DocX功能实现实例:

1.创建图表:

/// <summary> /// 创建棒形图 /// </summary> /// <param>文档路径</param> /// <param>绑定数据</param> /// <param>类别名称</param> /// <param>值名称</param> /// <param>图标标题</param> public static bool BarChart(string path,Dictionary<string, ICollection> dicValue,string categoryName,string valueName,string title) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } if (dicValue == null) { throw new ArgumentNullException("dicValue"); } if (string.IsNullOrEmpty(categoryName)) { throw new ArgumentNullException(categoryName); } if (string.IsNullOrEmpty(valueName)) { throw new ArgumentNullException(valueName); } if (string.IsNullOrEmpty(title)) { throw new ArgumentNullException(title); } try { using (var document = DocX.Create(path)) { //BarChart图形属性设置,BarDirection图形方向枚举,BarGrouping图形分组枚举 var c = new BarChart { BarDirection = BarDirection.Column, BarGrouping = BarGrouping.Standard, GapWidth = 400 }; //设置图表图例位置 c.AddLegend(ChartLegendPosition.Bottom, false); //写入图标数据 foreach (var chartData in dicValue) { var series = new Series(chartData.Key); series.Bind(chartData.Value, categoryName, valueName); c.AddSeries(series); } // 设置文档标题 document.InsertParagraph(title).FontSize(20); document.InsertChart(c); document.Save(); return true; } } catch (Exception ex) { throw new Exception(ex.Message); } }

2.创建一个具有超链接、图像和表的文档。

/// <summary> /// 创建一个具有超链接、图像和表的文档。 /// </summary> /// <param>文档保存路径</param> /// <param>加载的图片路径</param> /// <param>url地址</param> public static void HyperlinksImagesTables(string path,string imagePath,string url) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(path); } if (string.IsNullOrEmpty(imagePath)) { throw new ArgumentNullException(imagePath); } if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException(url); } try { using (var document = DocX.Create(path)) { var link = document.AddHyperlink("link", new Uri(url)); var table = document.AddTable(2, 2); table.Design = TableDesign.ColorfulGridAccent2; table.Alignment = Alignment.center; table.Rows[0].Cells[0].Paragraphs[0].Append("1"); table.Rows[0].Cells[1].Paragraphs[0].Append("2"); table.Rows[1].Cells[0].Paragraphs[0].Append("3"); table.Rows[1].Cells[1].Paragraphs[0].Append("4"); var newRow = table.InsertRow(table.Rows[1]); newRow.ReplaceText("4", "5"); var image = document.AddImage(imagePath); var picture = image.CreatePicture(); picture.Rotation = 10; picture.SetPictureShape(BasicShapes.cube); var title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS")); title.Alignment = Alignment.center; var p1 = document.InsertParagraph(); p1.AppendLine("This line contains a ").Append("bold").Bold().Append(" word."); p1.AppendLine("Here is a cool ").AppendHyperlink(link).Append("."); p1.AppendLine(); p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?"); p1.AppendLine(); p1.AppendLine("Can you check this Table of figures for me?"); p1.AppendLine(); p1.InsertTableAfterSelf(table); var p2 = document.InsertParagraph(); p2.AppendLine("Is it correct?"); document.Save(); } } catch (Exception ex) { throw new Exception(ex.Message); } }

 3.将指定内容写入文档:

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

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