NOPI读取Word模板并保存

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using NPOI.OpenXmlFormats.Wordprocessing;
using NPOI.XWPF.UserModel;

namespace WebDemo
{
    public partial class DocTest : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Export();
        }

        public static void Export()
        {
            string filepath = HttpContext.Current.Server.MapPath("~/file/simpleTable.docx");
            Test tt = new Test { name = "南通软件工程师", age = 18 };
            using (FileStream stream = File.OpenRead(filepath))
            {
                XWPFDocument doc = new XWPFDocument(stream);
                //遍历段落                  
                foreach (var para in doc.Paragraphs)
                {
                    ReplaceKey(para, tt);
                }                    

                //遍历表格      
                var tables = doc.Tables;
                foreach (var table in tables)
                {
                    foreach (var row in table.Rows)
                    {
                        foreach (var cell in row.GetTableCells())
                        {
                            foreach (var para in cell.Paragraphs)
                            {
                                ReplaceKey(para, tt);
                            }
                        }
                    }
                }

                FileStream out1 = new FileStream(HttpContext.Current.Server.MapPath("~/simpleTable" + DateTime.Now.Ticks + ".docx"), FileMode.Create);
                doc.Write(out1);
                out1.Close();
            }
        }


        private static void ReplaceKey(XWPFParagraph para, object model)
        {
            string text = para.ParagraphText;
            var runs = para.Runs;
            string styleid = para.Style;
            //for (int i = 0; i < runs.Count; i++)
            //{
            //    var run = runs[i];
            //    text = run.ToString(); 
            //    Type t = model.GetType();
            //    PropertyInfo[] pi = t.GetProperties();
            //    foreach (PropertyInfo p in pi)
            //    {
            //        //$$与模板中$$对应,也可以改成其它符号,比如{$name},务必做到唯一
            //        if (text.Contains("$" + p.Name + "$"))
            //        {
            //            text = text.Replace("$" + p.Name + "$", p.GetValue(model, null).ToString());
            //        }
            //    } 
            //    runs[i].SetText(text, 0);
            //}


                text = String.Join("", runs.Select(x => x.Text));
                Type t = model.GetType();
                PropertyInfo[] pi = t.GetProperties();
                foreach (PropertyInfo p in pi)
                {
                    //$$与模板中$$对应,也可以改成其它符号,比如{$name},务必做到唯一
                    if (text.Contains("$" + p.Name + "$"))
                    {
                        text = text.Replace("$" + p.Name + "$", p.GetValue(model, null).ToString());
                    }
                }

                int length = runs.Count();
                for (int j = (length - 1); j >= 0; j--)
                {
                     para.RemoveRun(j);
                }

                //直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
                //所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
                para.InsertNewRun(0).SetText(text, 0);
        }
    }

    public class Test
    {
        public string name { get; set; }
        public int age { get; set; }
    }
}

如图:

这里写图片描述


这里写图片描述

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页