. NET using Npoi to read Word templates and replace keywords and download

Source: Internet
Author: User

Npoi is the. NET version of the POI project. Poi is an open source Java program that reads and writes Microsoft OLE2 component documents such as Excel, Word, and so on.

With Npoi you can read and write Word/excel documents on a machine that does not have Office installed or the appropriate environment

npoi:http://npoi.codeplex.com/

The following code is for reference only, please modify it according to the actual requirement.

         PublicMemoryStream Export () {stringfilepath = Server.MapPath ("/word/xmxx.docx"); using(FileStream stream =File.openread (filepath)) {xwpfdocument doc=NewXwpfdocument (stream); //traversing a paragraph                    foreach(varParainchDoc.                    Paragraphs) {Replacekey (para); }                    //Traversing Tables                    vartables =Doc.                    Tables; foreach(varTableinchtables) {                        foreach(varRowinchtable. Rows) {foreach(varCellinchrow. Gettablecells ()) {foreach(varParainchcell.                                Paragraphs) {Replacekey (para); }                            }                        }                    }                    using(MemoryStream ms =NewMemoryStream ()) {Doc.                        Write (MS); returnMS; }                }        }        Private voidReplacekey (xwpfparagraph para) {bll.xmxxbll xmxxbll=NewBLL.XMXXBLL (); MODEL.XMXX Model=NewModel.xmxx (); Model= Xmxxbll.getmodel ( -); stringText =para.            Paragraphtext; varRuns =para.            Runs; stringStyleid =para.            Style;  for(inti =0; I < runs. Count; i++)            {                varRun =Runs[i]; Text=run.                ToString (); Type T=model.                GetType (); propertyinfo[] Pi=t.getproperties (); foreach(PropertyInfo Pinchpi) {                    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 voidButton1_Click (Objectsender, 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 using Npoi to read Word templates and replace keywords and download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.