This is our requirement.
1. There is a template for such a Word document as follows:
2. We need to dynamically generate such a file on the server. Each time we need to change the content to the title, description, and time. Read the employee list and fill it in the following table.
Our solution is:
1. Install word on the server, and then operate and generate documents by using the com model of word, such as Word. Application and word.doc ument.
The problem with this method is that the server must install word, which may be impossible for many customers.
2. generate WORD Documents in XML format. This is what this log describes.
First, we will complete all the Word documents, including the format settings. As shown in.
Next, we will save the document in XML format
What about this document?
It looks messy, right? It doesn't matter. Let's take a look at the relevant architecture.
Open the file in IE. Delete the following sentence first.
In order to quickly modify and expand the document later, we add several tags to the XML element of the previously written text.
We added several tags
Tag = "title" indicates the title
Tag = "Description" indicates the description.
Tag = "time" indicates the time
Tag = "table" indicates the row of the table.
Tag = "firstname" indicates the surname
Tag = "lastname" indicates the name
Tag = "country" indicates country
Tag = "region" indicates the region
Tag = "city" indicates the city
Then, we need to write down a namespace
Xmlns: W ="Http://schemas.microsoft.com/office/word/2003/wordml"
Okay. With these materials, we can modify the file on the website.
We use a separate ashx to generate the file.
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Web; Using System. Web. Services; Using System. xml. LINQ; Using System. IO; Namespace Worddocumentweb { /// <Summary> /// $ Codebehindclassname $ abstract description /// </Summary> [WebService (namespace = "Http://tempuri.org /" )] [Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)] Public Class Documenthandler: ihttphandler { Public Void Processrequest (httpcontext context) {context. response. Clear (); context. response. addheader ( "Content-disposition" , "Attachment?filename=employeereport.doc" ); Context. response. contentencoding = system. Text. encoding. getencoding ( UTF-8" ); Context. response. contenttype = "Application/vnd. MS-word" ; Xdocument Doc = xdocument. Load (context. server. mappath ( "Employee template. xml" )); // Start modifying the document Xnamespace W = Http://schemas.microsoft.com/office/word/2003/wordml" ; Xnamespace wx = Http://schemas.microsoft.com/office/word/2003/auxHint" ; Var nodes = Doc. Root. element (W +"Body" ). Descendants (W + "T" ); Findandreplacenode (nodes, "Title" , "Document title (CodeModify )" ); Findandreplacenode (nodes, "Description" , "Document description (code modification )" ); Findandreplacenode (nodes, "Time" , Datetime. Now. tostring ()); // Fill the table VaR table = Doc. Root. element (W +"Body" ). Element (wx + "Sub-section" ). Element (W + "TBL" ); // This is a table VaR templaterow = Doc. Root. element (W + "Body" ). Descendants (W + "TR" ). Firstordefault (E => E. Attribute ( "Tag" )! = Null & E. Attribute ( "Tag" ). Value ="Table" ); For ( Int I = 0; I <10; I ++) {var newrow = templaterow. Clone (); var datas = newrow. descendants (W + "T" ); Findandreplacenode (datas, "Firstname" , "Chen" ); Findandreplacenode (datas, "Lastname" , "Xi Zhang" ); Findandreplacenode (datas,"Country" , "China" ); Findandreplacenode (datas, "Region" , "Shanghai" ); Findandreplacenode (datas, "City" , "Shanghai" ); Table. Add (newrow);} templaterow. Remove (); streamwriter Sw = New Streamwriter (context. response. outputstream); Doc. Save (SW); Sw. Flush (); Sw. Close (); context. response. End ();}Private Void Findandreplacenode (ienumerable <xelement> elements, String Tag, String Value ) {Var found = elements. firstordefault (n => N. Attribute ( "Tag" )! = Null & N. Attribute ( "Tag" ). Value = tag ); If (Found! = Null ) Found. value = Value ;} Public Bool Isreusable {get { Return False ;}}} Public Static Class Extensions { /// <Summary> /// Clone a node /// </Summary> /// <Param name = "element"> </param> /// <Returns> </returns> Public Static Xelement clone ( This Xelement ){ Return New Xelement (element. Name, element. attributes (), element. nodes (). Select (n => {xelement E = N As Xelement; If (E! = Null ){ Return E. Clone ();} Return N ;}),(! Element. isempty &&! Element. nodes (). oftype <xtext> (). Any ())? String . Empty: Null );}}}
Next, in the page, our code is
Protected VoidBtgenerate_click (ObjectSender, eventargs e ){// Generate a Word document based on the template. The replaced part includes the title text, description text, and table.Response. Redirect ("Kerberenthandler. ashx");}
The final running result is as follows: