C # WebForm uses Npoi 2 to generate a simple Word document (. docx)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.