The use of Npoi can easily implement the service side of Word, Excel read and write. To implement read-write operations on Word, you need to reference NPOI.OOXML.dll and apply the namespace XWPF.
This article uses Npoi 2.0 to implement basic build, download operations for Word. Nopi 2.0:http://npoi.codeplex.com/downloads/get/764162
Although the latest version is Npoi 2.1.1, but I use 2.1.1 NPOI.OOXML.dll, I found that the paragraph formatting can not be implemented, it is recommended to use this version.
Namespaces that need to be added:
1 using System.IO; 2 using Npoi. XWPF. Usermodel;
Code:
1Xwpfdocument doc =NewXwpfdocument ();//Create a new Word document2 3Xwpfparagraph P1 = doc. Createparagraph ();//add a paragraph to a new document4P1. Setalignment (Paragraphalignment.center);//The paragraph is centered on its way5 6Xwpfrun r1 = p1. Createrun ();//add text to this paragraph7R1. SetText ("Test paragraph one");8 9Xwpfparagraph P2 =Doc. Createparagraph ();Ten P2. Setalignment (paragraphalignment.left); One AXwpfrun r2 =P2. Createrun (); -R2. SetText ("Test paragraph two"); - the -FileStream SW = File.create ("Cutput.docx");//... -Doc. Write (SW);//... -Sw. Close ();//generating files on the server + -FileInfo file =NewFileInfo ("Cutput.docx");//file save path and name + //Note: The parent folder where the file is saved needs to be added to the Everyone user and given Full Control permissions A response.clear (); at response.clearheaders (); -Response.Buffer =false; -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 ()); in response.writefile (file. FullName); -Response.Flush ();//The above will send the generated Word file to the user's browser to +File.delete ("Cutput.docx");//clears the Word file generated by the server
Page effect:
Generated word:
C # WebForm uses Npoi 2 to generate a simple Word document (. docx)