码迷,mamicode.com
首页 > Web开发 > 详细

Asponse.Words进行Word替换

时间:2017-10-30 19:53:45      阅读:625      评论:0      收藏:0      [点我收藏+]

标签:.text   void   com   开始   es2017   raw   namespace   system   int   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;
using Aspose.Words;
using Aspose.Words.Drawing;
using System.IO;


namespace ASPONSE_Words
{
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("开始进行操作");
            DataTable dt = new DataTable();
            dt.Columns.Add("title", typeof(string));
            dt.Columns.Add("xm", typeof(string));
            dt.Columns.Add("xb", typeof(string));
            dt.Columns.Add("mz", typeof(string));
            dt.Columns.Add("nl", typeof(string));
            dt.Columns.Add("Photo1", typeof(string));
            dt.Columns.Add("Photo2", typeof(string));
            dt.Columns.Add("Photo3", typeof(string));
            dt.Columns.Add("Photo4", typeof(string));
            DataRow dr = dt.NewRow();
            dr["title"] = "测试";
            dr["xm"] = "华哥";
            dr["xb"] = "男神";
            dr["mz"] = "汉族";
            dr["nl"] = "18";
            dr["Photo1"] = "../File/1.jpg";
            dr["Photo2"] = "../File/2.jpg";
            dr["Photo3"] = "../File/3.jpg";
            dr["Photo4"] = "../File/4.jpg";
            dt.Rows.Add(dr);
            var fileUrl = "../File/test11.doc";
            try
            {
                Print(fileUrl, dt);
                Console.WriteLine("替换成功");
            }
            catch (Exception)
            {
                Console.WriteLine("替换失败");
            }

            Console.ReadKey();
        }

        public static void Print(string fileurl, DataTable dtInfo)
        {
            Document doc = new Document(fileurl);
            if (dtInfo != null && dtInfo.Rows.Count > 0)
            {
                var dr = dtInfo.Rows[0];
                foreach (DataColumn dc in dtInfo.Columns)
                {
                    var nValue = dr[dc.ColumnName] + "";
                    try
                    {
                        if (dc.ColumnName.Contains("Photo"))
                        {
                            if (File.Exists(nValue))
                            {
                                Regex reg = new Regex("#" + dc.ColumnName + "#");
                                doc.Range.Replace(reg, new ReplaceImage2(nValue), false);
                            }
                        }
                        else doc.Range.Replace("$"+dc.ColumnName+"$", nValue, false, false);
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            doc.Save("ceshi.doc");
        }
    }

    public class ReplaceImage1 : IReplacingCallback
    {
        public string imageUrl;

        public ReplaceImage1(string url)
        {
            this.imageUrl = url;
        }

        public ReplaceAction Replacing(ReplacingArgs e)
        {
            //获取当前节点
            var node = e.MatchNode;
            //获取当前文档
            Document doc = node.Document as Document;
            DocumentBuilder builder = new DocumentBuilder(doc);
            //将光标移动到指定节点
            builder.MoveTo(node);
            //插入图片
            builder.InsertImage(imageUrl);
            return ReplaceAction.Replace;
        }

    }

    public class ReplaceImage2 : IReplacingCallback
    {
        public string imageUrl;

        public ReplaceImage2(string url)
        {
            this.imageUrl = url;
        }

        public ReplaceAction Replacing(ReplacingArgs e)
        {
            //获取当前节点        
            if (!string.IsNullOrEmpty(imageUrl))
            {
                var node = e.MatchNode;
                Document doc = node.Document as Document;
                DocumentBuilder builder = new DocumentBuilder(doc);
                builder.MoveTo(node);
                Shape shape = new Shape(doc, ShapeType.Image);
                shape.ImageData.SetImage(imageUrl);
                shape.Top = 0;
                shape.Width = 80;
                shape.Height = 100;
                shape.HorizontalAlignment = HorizontalAlignment.Center;
                CompositeNode node1 = shape.ParentNode;
                builder.InsertNode(shape);
            }
            return ReplaceAction.Replace;
        }

    }
}

模板:

技术分享

效果图:

技术分享

 

Asponse.Words进行Word替换

标签:.text   void   com   开始   es2017   raw   namespace   system   int   

原文地址:http://www.cnblogs.com/huage-1234/p/7755670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!