一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - ASP.NET教程 - 詳解一款開源免費的.NET文檔操作組件DocX(.NET組件介紹之一)

詳解一款開源免費的.NET文檔操作組件DocX(.NET組件介紹之一)

2020-04-13 12:38彭澤0902 ASP.NET教程

本篇文章主要是介紹了一款開源免費的.NET文檔操作組件DocX,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

在目前的軟件項目中,都會較多的使用到對文檔的操作,用于記錄和統計相關業務信息。由于系統自身提供了對文檔的相關操作,所以在一定程度上極大的簡化了軟件使用者的工作量。

在.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():創建文檔。

?
1
2
3
4
5
6
7
8
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:向段落添加信息。

?
1
2
3
4
5
6
7
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;
}
?
1
2
3
4
5
public Paragraph Bold()
{
  this.ApplyTextFormattingProperty(XName.Get("b", DocX.w.NamespaceName), string.Empty, null);
  return this;
}

3.Table.InsertTableAfterSelf:將數據插入表格。

?
1
2
3
4
5
6
7
8
9
10
11
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:自定義屬性。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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:創建棒形圖。

?
1
2
3
4
5
6
7
8
9
10
11
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; }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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()方法解析:
 

?
1
2
3
4
5
6
7
8
9
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);
}
?
1
2
3
4
5
6
7
8
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);
}
?
1
2
3
4
5
public void RemoveLegend()
{
  this.Legend.Xml.Remove();
  this.Legend = null;
}

以上是對DocX組件的一些方法的一些簡單解析,如果需要知道更多的方法實現代碼,可自行進行下載查看。

三.DocX功能實現實例:

1.創建圖表:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// <summary>
   /// 創建棒形圖
   /// </summary>
   /// <param name="path">文檔路徑</param>
   /// <param name="dicValue">綁定數據</param>
   /// <param name="categoryName">類別名稱</param>
   /// <param name="valueName">值名稱</param>
   /// <param name="title">圖標標題</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.創建一個具有超鏈接、圖像和表的文檔。

?
<label id="qneci"><table id="qneci"></table></label>

  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    /// <summary>
       /// 創建一個具有超鏈接、圖像和表的文檔。
       /// </summary>
       /// <param name="path">文檔保存路徑</param>
       /// <param name="imagePath">加載的圖片路徑</param>
       /// <param name="url">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 id="codetool">

     3.將指定內容寫入文檔:

    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    /// <summary>
       /// 將指定內容寫入文檔
       /// </summary>
       /// <param name="path">加載文件路徑</param>
       /// <param name="content">寫入文件內容</param>
       /// <param name="savePath">保存文件路徑</param>
       public static void ProgrammaticallyManipulateImbeddedImage(string path, string content, string savePath)
       {
         if (string.IsNullOrEmpty(path))
         {
           throw new ArgumentNullException(path);
         }
         if (string.IsNullOrEmpty(content))
         {
           throw new ArgumentNullException(content);
         }
         if (string.IsNullOrEmpty(savePath))
         {
           throw new ArgumentNullException(savePath);
         }
         try
         {
           using (var document = DocX.Load(path))
           {
             // 確保此文檔至少有一個圖像。
             if (document.Images.Any())
             {
               var img = document.Images[0];
               // 將內容寫入圖片.
               var b = new Bitmap(img.GetStream(FileMode.Open, FileAccess.ReadWrite));
               //獲取此位圖的圖形對象,圖形對象提供繪圖功能。
               var g = Graphics.FromImage(b);
               // 畫字符串內容
               g.DrawString
                 (
                   content,
                   new Font("Tahoma", 20),
                   Brushes.Blue,
                   new PointF(0, 0)
                 );
               // 使用創建\寫入流將該位圖保存到文檔中。
               b.Save(img.GetStream(FileMode.Create, FileAccess.Write), ImageFormat.Png);
             }
             else
             {
               document.SaveAs(savePath);
             }
           }
     
         }
         catch (Exception ex)
         {
           throw new Exception(ex.Message);
         }
       }

    四.總結:

    以上是對DocX組件的API做了一個簡單的解析,并且附上一些創建文檔和創建圖表的方法供開發者參考。希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

    原文鏈接:http://www.cnblogs.com/pengze0902/p/6122311.html

    延伸 · 閱讀

    精彩推薦
    主站蜘蛛池模板: 视频在线视频免费观看 | 欧美调教打屁股spank视频 | 美女扒开屁股让男人进去 | 日本爽p大片免费观看 | 亚洲XXX午休国产熟女屁 | 久久WWW免费人成一看片 | 男人天堂网www | 日韩去日本高清在线 | 456在线观看 | 国产福利一区二区精品视频 | 女同变态 中文字幕 | 国产91素人搭讪系列天堂 | 美女毛片在线 | 国产一区二区三区久久小说 | 久久久96| 日本高清免费不卡在线 | 99精品偷自拍 | 成 人 免费 小说在线观看 | 亚洲AV午夜福利精品香蕉麻豆 | 男人的影院 | 欧美成人v视频免费看 | 五月婷婷俺也去开心 | 高h辣文小说网 烧书阁 | 国产精品视频播放 | 洗濯屋H纯肉动漫在线观看 武侠艳妇屈辱的张开双腿 午夜在线观看免费观看 视频 | 无码乱人伦一区二区亚洲一 | ass巨大胖女人sias | 国产精品igao视频网网址 | 成人性色生活片免费网 | 干美女视频 | 国产尤物视频 | 国产一区二区免费福利片 | 成人免费高清视频 | 久久99精品国产自在自线 | 91麻豆精品国产片在线观看 | 日本xxxxxxxxx高清hd | 亚洲福利视频在线观看 | 亚洲高清成人 | 国产午夜精品一区二区三区不卡 | 强漂亮白丝女教师小说 | 午夜dj影院在线观看完整版 |
  • <blockquote id="qneci"></blockquote>

      <blockquote id="qneci"><samp id="qneci"></samp></blockquote>