Using Npoi. XWPF. Usermodel
1 xwpfdocument doc = new xwpfdocument (); Create a new Word document 2 3 xwpfparagraph p1 = doc. Createparagraph (); Add paragraph 4 p1 to the new document. Setalignment (Paragraphalignment.center); The paragraph is centered on its way 5 6 xwpfrun r1 = p1. Createrun (); Add text 7 r1 to the paragraph. SetText ("test paragraph One"); 8 9 Xwpfparagraph p2 = doc. Createparagraph (); ten P2. Setalignment (paragraphalignment.left); Xwpfrun r2 = p2. Createrun (); R2. SetText ("Test paragraph II"); R2. Setfontsize (16);//Set Font size
R2. Setblod (TRUE);//Set Bold
FileStream SW = file.create ("Cutput.docx"); //... Doc. Write (SW); //... SW. Close (); Generate files on the server FileInfo file = new FileInfo ("Cutput.docx");//File save path and name 21 Note: The parent folder where the file is saved needs to be added to the Everyone user and given full control privileges of response.clear (); Response.ClearHeaders (); 24 Response.Buffer = false;25 Response.ContentType = "Application/octet-stream"; response.appendheader ("Content-disposition", "attachment;filename=" + httputility.urlencode ("Output.docx", System.Text.Encoding. UTF8)); Response.appendheader ("Content-length", file. Length.tostring ()); Response.WriteFile (file. FullName); Response.Flush (); The above will generate the word file sent to the user browser file.delete ("Cutput.docx");
C # Build Word document (Npoi)