There are many well-known npoi text Gardens used to export Excel files, but most of the articles used to export Word documents are vague. Recently, we just completed the need to export a Word document.
In npoi, the most basic structure of a Word document is a paragraph. The class representing this paragraph is xwpfparagraph. You can use this class to set the font, size, and whether to bold the paragraph.
The xwpfdocument class represents the entire document. The outermost container is equivalent to an example of a Word document, the table class xwpftable, and a small paragraph container is equivalent to an instance of a table.
One xwpfdocument can add multiple xwpftable and multiple xwpfparagraph, and one xwpftable can add multiple xwpfparagraph
Now we need to create a Word document with several paragraphs and tables. The text in the form must be formatted (, ), as shown in
First, you need to create an xwpfdocument Doc = new xwpfdocument (), and then use the document instance Doc to create section 1 ~ Section N
1 xwpfparagraph p0 = Doc. createparagraph (); 2. setalignment (paragraphalignment. left); 3 xwpfrun R0 = descricreaterun (); 4 r0.setfontfamily (" ily"); 5 r0.setfontsize (18); 6 r0.setbold (true ); 7 r0.settext ("account and password of a student not logged on"); 8 9 xwpfparagraph p1 = Doc. createparagraph (); 10 p1.setalignment (paragraphalignment. left); 11 xwpfrun R1 = p1.createrun (); 12 r1.setfontfamily (""); 13 r1.setfontsize (10); 14 r1.setbold (true); 15 r1.settext ("(Remarks: "); 16 17 xwpfparagraph P2 = Doc. createparagraph (); 18 p2.setalignment (paragraphalignment. left); 19 xwpfrun r2 = p2.createrun (); 20 r2.setfontfamily (""); 21 r2.setfontsize (10); 22 r2.setbold (true); 23 r2.settext ("school: XX medium "); 24 25 xwpfparagraph P3 = Doc. createparagraph (); 26 p3.setalignment (paragraphalignment. left); 27 xwpfrun R3 = p3.createrun (); 28 r3.setfontfamily (""); 29 r3.setfontsize (10); 30 r3.setbold (true); 31 r3.settext ("class :( 7) "); 32 33 xwpfparagraph P4 = Doc. createparagraph (); 34 p4.setalignment (paragraphalignment. left); 35 xwpfrun r4 = p4.createrun (); 36 r4.setfontfamily (""); 37 r4.setfontsize (10); 38 r4.setbold (true); 39 r4.settext ("class teacher: DDD "); 40 41 42 xwpfparagraph P5 = Doc. createparagraph (); 43 p5.setalignment (paragraphalignment. left); 44 xwpfrun R5 = p5.createrun (); 45 r5.setfontfamily (""); 46 r5.setfontsize (10); 47 r5.setbold (true ); 48 r5.settext ("remarks can be added here :");
Then create four rows and four columns of xwpftable table = Doc. createtable (4, 4) with Doc)
Next, many people simply set the constructed section based on the method shown by vs or the method set by the npoi Original Author tonyqus's Tutorial example () method. However, it turns out that this is wrong, in this way, the constructed paragraphs are not only displayed in the table, but also in other places. The correct method should be to directly generate a table-specific section using the addparagraph () method of the table cell.
1 xwpfparagraph Pio = table. getrow (I ). getcell (0 ). addparagraph (); 2 xwpfrun Rio = Pio. createrun (); 3 Rio. setfontfamily (""); 4 Rio. setfontsize (12); 5 Rio. setbold (true); 6 Rio. settext (I. tostring (); 7 8 9 xwpfparagraph Pino = table. getrow (I ). getcell (1 ). addparagraph (); 10 xwpfrun Rino = Pino. createrun (); 11 Rino. setfontfamily (""); 12 Rino. setfontsize (12); 13 Rino. setbold (true); 14 Rino. settext (notloginstudents [I-1]. username); 15 16 17 xwpfparagraph Pimm = table. getrow (I ). getcell (2 ). addparagraph (); 18 xwpfrun Rimm = Pimm. createrun (); 19 rimm. setfontfamily (""); 20 rimm. setfontsize (12); 21 rimm. setbold (true); 22 rimm. settext (notloginstudents [I-1]. password); 23 24 25 xwpfparagraph piname = table. getrow (I ). getcell (3 ). addparagraph (); 26 xwpfrun riname = piname. createrun (); 27 riname. setfontfamily (""); 28 riname. setfontsize (12); 29 riname. setbold (true); 30 riname. settext (notloginstudents [I-1]. studentname );
Then, write the document to the stream Doc. Write (New memorystream (), and then you will be OK to do whatever you want.
Npoi 2.0 export word (docx format)