NPOI 2.0 export word (docx format), npoidocx
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.
How does one export word from NPOI?
We recommend that you use a third-party: PageOffice. This is an excellent job. You can install it in the asp.net toolbox and use the standard C # control. It is very easy to call the code. You can export Word and excel files, and export text, images, and table content. You can set the format, which does not occupy server resources during export, and supports concurrent web access.
If you have any questions, continue to ask. Thank you.
I hope the answer is helpful to you. If you have any questions, please continue to "Ask"
It is difficult to answer questions and understand each other. Your adoption is the driving force for me to move forward. Thank you.
When NPOI (20) is changed to Excel 07, the style will be lost after being saved?
Check whether there are any new DLL releases on the official website. I am using 2.0.1.0