【.Net 6.0--通用帮助类--WordHelper】

前言

Word帮助类,无需安装office即可使用,包含了插入段落到word文件(InsertParagraphs)、保存datatable到word文件中的指定表格(InsertRows)、插入图片到word文件中的特定位置(InsertPictures)、替换文字到word文件中的特定位置(InsertTexts)等方法。

需要引用Aspose.Words,依赖文件,我已共享资源,请需要的同学自行下载。


代码示例

using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Replacing;
using System.Collections.Generic;
using System.Drawing;

namespace VW.API.Common.Utils
{
    #region Models
    /// 
    /// 段落Model
    /// 
    public class WordParagraphModel
    {
        public string Value { set; get; }
        public Color Color { set; get; } = Color.Black;
        public double Size { set; get; } = 14;
        public bool Bold { set; get; } = false;
        public bool WriteIn { set; get; } = false;//是否换行
    }

    /// 
    /// 图片Model
    /// 
    public class WordPictureModel
    {
        public string Value { set; get; }
        public double Width { set; get; } = 200.0;
        public double Height { set; get; } = 200.0;
    }

    /// 
    /// 表格Model
    /// 
    public class WordTableModel
    {
        public List> Value { set; get; } = new List>();
        public int DeleteRowNum { set; get; } = -1;
    }
    #endregion

    /// 
    /// WordHelper 的摘要说明:Word帮助类
    /// 
    public static class WordHelper
    {
        /// 
        /// 替换指定内容
        /// 
        /// 
        /// 
        public static void InsertTexts(string filePath, Dictionary texts)
        {
            Document doc = new Document(filePath);

            foreach (KeyValuePair text in texts)
            {
                doc.Range.Replace(text.Key, text.Value, new FindReplaceOptions
                {
                    MatchCase = false,
                    FindWholeWordsOnly = false
                });
            }

            doc.Save(filePath);
        }

        /// 
        /// 插入图片
        /// 
        /// 
        /// 
        public static void InsertPictures(string filePath, Dictionary dicPictures)
        {
            Document doc = new Document(filePath);

            DocumentBuilder builder = new DocumentBuilder(doc);
            foreach (KeyValuePair keyValue in dicPictures)
            {
                builder.MoveToMergeField(keyValue.Key);
                builder.InsertImage(keyValue.Value.Value,
                    RelativeHorizontalPosition.Margin,
                    0,
                    RelativeVerticalPosition.Margin,
                    0,
                    builder.CellFormat.Width,
                    keyValue.Value.Height,
                    WrapType.TopBottom);
            }

            doc.Save(filePath);
        }

        /// 
        /// 表格插入行记录
        /// 
        /// 
        /// 
        public static void InsertRows(string filePath, Dictionary dicTables)
        {
            Document doc = new Document(filePath);

            NodeCollection allTables = doc.GetChildNodes(NodeType.Table, true);
            foreach (KeyValuePair keyValue in dicTables)
            {
                Aspose.Words.Tables.Table table = allTables[keyValue.Key] as Aspose.Words.Tables.Table;
                foreach (List value in keyValue.Value.Value)
                {
                    Aspose.Words.Tables.Row clonedRow = (Aspose.Words.Tables.Row)table.LastRow.Clone(true);
                    for (int i = 0; i < clonedRow.Cells.Count; i++)
                    {
                        clonedRow.Cells[i].FirstParagraph.ChildNodes.Clear();
                        clonedRow.Cells[i].FirstParagraph.ChildNodes.Add(new Run(clonedRow.Cells[i].Document, value[i]));
                    }
                    table.AppendChild(clonedRow);
                }

                if (keyValue.Value.DeleteRowNum != -1)
                    table.Rows.RemoveAt(keyValue.Value.DeleteRowNum);

                table.AllowAutoFit = false;
            }

            doc.Save(filePath);
        }

        /// 
        /// 插入段落
        /// 
        /// 
        /// 
        public static void InsertParagraphs(string filePath, Dictionary> dicParagraphs)
        {
            Document doc = new Document(filePath);

            DocumentBuilder builder = new DocumentBuilder(doc);
            foreach (KeyValuePair> keyValue in dicParagraphs)
            {
                builder.MoveToMergeField(keyValue.Key);

                foreach (var paragraphModel in keyValue.Value)
                {
                    builder.Font.Bold = paragraphModel.Bold;
                    builder.Font.Size = paragraphModel.Size;
                    builder.Font.Color = paragraphModel.Color;
                    if (paragraphModel.WriteIn)
                        builder.Writeln(paragraphModel.Value);
                    else
                        builder.Write(paragraphModel.Value);
                }
            }

            doc.Save(filePath);
        }
    }
}

你可能感兴趣的:(.Net,通用帮助类,.net,c#,开发语言,visual,studio,学习)