Download Npoi2.0npoi: Click the open link Word template based on word2007 and above. Template Final Effect
Replace paragraph 1, open Word template
Word.openword (context. Server.MapPath (@ "~/test/wordhelper/template/template. docx"));
2. Paragraph replacement
Dictionary<string, object> d = new dictionary<string, object> (); Paragraph d = new dictionary<string, object> (); D.add ("No", DateTime.Now.ToString ()); Word.setcellvalueparagraphs (d);
Cell partial text substitution in table
Table interior cell layout replace d = new dictionary<string, object> (); D.add ("Tableusername", "Zhengzhou Zhang San"); Word.setcellvaluettables (d, 0);
This feature is primarily used to replace a cell part. For example, the contents of the template cell are: "Name: Tableusername", just replace Tableusername Note: There is a bug here and the placeholder cannot be found if it cannot be parsed by Npoi as a run unit. For example tableusername may be parsed into Table+user+name 3 run the last parameter is the table index, you can get all the tables in the Word template by Word.tables ()
Cell full text substitution in table
Table inside cell Gecquan Replace d = new dictionary<string, object> (); D.add ("#tableuserid $", "001"); D.add ("#tableusername $", "Zhengzhou Zhang San"); D.add ("#img $", "$img {" + context.) Server.MapPath (@ "~/test/wordhelper/cat. jpg") + "} (5000000,5000000)"); Insert Picture (width, height) word.setcellvaluettables1 (d, 1);
This feature replaces the value of the entire cell. Therefore, only placeholders in the cell are required, and no other text can be left. The placeholder is also added with the # placeholder $ to guarantee uniqueness and readability of the cell placeholder
Insert a picture in a table
D.add ("#img $", "$img {" + context.) Server.MapPath (@ "~/test/wordhelper/cat. jpg") + "} (5000000,5000000)"); Insert Picture (width, height) word.setcellvaluettables1 (d, 1);
Placeholders for inserting pictures can be used with a table operation. Parameter description: $img {picture absolute position} (width, height). $img {} is used to indicate that this is a picture operation
Table Loops Insert Multiple rows
Final effect
Circular table DataSet ds_table = new DataSet (); DataTable dt_table = new DataTable (); Dt_table. Columns.Add ("Itemid"); Dt_table. Columns.Add ("ItemName"); for (int i = 0; i < 5; i++) { DataRow dr = Dt_table. NewRow (); dr["itemid"] = i.ToString (); dr["ItemName"] = i.tostring () + "name"; Dt_table. Rows.Add (DR); } Ds_table. Tables.add (dt_table); Word.setcellvaluettables (ds_table, 1, word.tables[2]);
Similar to Excel, the order in which a multiline loop inserts records from a dataset into a table,dataset column can be inconsistent with the column order of the template, and the program automatically matches the corresponding column according to its name.Sample Download
Resource Address
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Npoi Exporting word2007 helper Classes