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