.NET使用NPOI读取Word模板并替换关键字并下载

NPOI 是 POI 项目的 .NET 版本。POI是一个开源的Java读写Excel、WORD等微软OLE2组件文档的项目。

使用 NPOI 你就可以在没有安装 Office 或者相应环境的机器上对 WORD/EXCEL 文档进行读写

NPOI下载地址:http://npoi.codeplex.com/

以下代码仅供参考,请根据实际需求进行修改。

        public MemoryStream Export()
        {
  
                string filepath = Server.MapPath("/word/xmxx.docx");
                using (FileStream stream = File.OpenRead(filepath))
                {
                    XWPFDocument doc = new XWPFDocument(stream);
                    //遍历段落
                    foreach (var para in doc.Paragraphs)
                    {
                        ReplaceKey(para);
                    }
                    //遍历表格
                    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);
                                }
                            }
                        }
                    }
                    using (MemoryStream ms = new MemoryStream())
                    {
                        doc.Write(ms);
                        return ms;
                    }
                }

        }
        private void ReplaceKey(XWPFParagraph para)
        {
            BLL.XmxxBLL XmxxBLL = new BLL.XmxxBLL();
            Model.Xmxx model = new Model.Xmxx();
            model = XmxxBLL.GetModel(20);

            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)
                {
                    if (text.Contains("{$xmxx." + p.Name + "}"))
                    {
                        text = text.Replace("{$xmxx." + p.Name + "}", TM.Common.StringHelper.ToString(p.GetValue(model, null)));
                    }
                }
                runs[i].SetText(text, 0);
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (MemoryStream ms = Export())
            {
                Response.ContentType = "application/vnd.ms-word";
                Response.ContentEncoding = Encoding.UTF8;
                Response.Charset = "";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("123.doc", Encoding.UTF8));
                Response.BinaryWrite(Export().GetBuffer());
                Response.End();
            }
        }

 

posted @ 2015-08-19 10:14  唐学阳  阅读(8703)  评论(1编辑  收藏  举报