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

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

时间:2015-08-19 11:01:22      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

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();
            }
        }

 

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

标签:

原文地址:http://www.cnblogs.com/tangxueyang/p/4741430.html

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