Reproduced Java generates Word documents

Source: Internet
Author: User

In the process of developing a document system or Office system, sometimes we need to export Word documents. Found on the Internet a Word file using Pageoffice to generate the function, the piece out to share with you.

Building a Word file is essentially the same as the Word document we're editing, except that it's replaced with code when it comes to creating a Word document with a program. In the following examples, we have added headings, text (paragraphs, fonts, font size, line spacing, alignment, first line indentation, etc.), inserting pictures, and so on. The examples given here are just some of the ways in which the Pageoffice component generates Word files, whether you need to use more methods or to see the API yourself according to the actual requirements. API Address: http://www.zhuozhengsoft.com/help/java3/index.html

View the objects and methods under the Com.zhuozhengsoft.pageoffice.wordwriter package that are used to generate the Word file

  

Let's go straight to the code:

 1 Pageofficectrl poCtrl1 = new Pageofficectrl (request);  2 Poctrl1.setserverpage (Request.getcontextpath () + "/poserver.zz"); 3//Create Worddocument object 4 worddocument doc = new worddocument (); 5//Set Content Title 6//Create DataRegion object, Po_title is automatically added bookmark name, book signature should be "Po_" as the prefix, cut bookmark name cannot repeat 7//three parameters are the name of the new bookmark to be inserted, the insertion position of the new bookmark, the associated bookmark Name ("[Home]" represents the first position of a Word document) 8 dataregion title = Doc.createdataregion ("Po_title", 9 dataregioninserttype.af ter, "[Home]"); 10//Assign value to DataRegion object one Title.setvalue ("C # socket multithreaded programming instance \ n"); 12//Set Font: Weight, size, font name, whether Italic ti Tle.getfont (). Setbold (True); Title.getfont (). SetSize (), Title.getfont () setName ("blackbody"); Title.getfont () . Setitalic (false); 17//Define paragraph object ParagraphFormat Titlepara = Title.getparagraphformat (); 19//Set paragraph alignment tit Lepara.setalignment (Wdparagraphalignment.wdalignparagraphcenter); 21//Set paragraph line spacing of Titlepara.setlinespacingrule ( Wdlinespacing.wdlinespacemultiple); 23 24//Set content 25//First paragraph 26//Create DataRegion object, Po_body for automatically added bookmark name DataRegion BODY = doc.createdataregion ("Po_body", Dataregioninserttype.after, " Po_title "); 28//Set Font: Weight, italic, size, font name, font color Body.getfont (). Setbold (false); Body.getfont (). Setitalic (True); 3 1 Body.getfont (). SetSize (10); 32//Set Chinese font name Body.getfont (). SetName ("italics"); 34//Set English font name Body.getfont (). SetName ("Times New Roman"), Body.getfont () setcolor (color.red); 37//Assign DataRegion object to Body.setvalue ("is Microsoft with A new language for vs.net. As a new language, it has a strong C + +, and has the rad characteristics of VB. Moreover, Microsoft's main purpose in C # is to fight Sun's java. We all know the powerful features of the Java language, especially in the area of network programming. As a result, C # in the network programming is also naturally not behind people. This paper introduces some basic knowledge of implementing socket (Sockets) programming under C #, so that we can have a general understanding of this. First, let me introduce you to the concept of sockets.      \ n "); 39//Create ParagraphFormat object ParagraphFormat Bodypara = Body.getparagraphformat (); 41//Set line spacing, alignment, first line indent 42 in paragraph Bodypara.setlinespacingrule (Wdlinespacing.wdlinespaceatleast); Bodypara.setalignment ( Wdparagraphalignment.wdalignparagraphleft); bodypara.setfirstlineindent (21); 45 46//second paragraph 47    DataRegion body2 = doc.createdataregion ("Po_body2", Dataregioninserttype.after, "Po_body"), Body2.getfont (). SetBo LD (FALSE); Body2.getfont (). SetSize (), Body2.getfont () setName ("blackbody"); Body2.setvalue ("Socket is the cornerstone of communication, is to support tcp/ The basic operating unit for network communication of IP protocols. Sockets can be considered as endpoints of two-way communication between processes in different hosts, which form a programming interface between a single host and the entire network. Sockets exist in the communication domain, and the communication domain is an abstract concept that is introduced in order to deal with the common thread through socket communication. Sockets typically exchange data with sockets in the same domain (data exchange may also traverse the boundaries of a domain, but it is important to perform some sort of interpreter). Various processes use the same domain to communicate with each other using an Internet Protocol cluster. \ n ");//body2.setvalue (" [Image]: /images/logo.jpg[/image] "); ParagraphFormat BodyPara2 = Body2.getparagraphformat (); Bodypara2.setlinespacingru     Le (WDLINESPACING.WDLINESPACE1PT5); bodypara2.setalignment (wdparagraphalignment.wdalignparagraphleft); 56 Bodypara2.setfirstlineindent (21); 57 58//Third paragraph dataregion body3 = Doc.createdataregion ("Po_body3", Dataregioninser Ttype.after, "Po_body2"); Body3.getfont (). Setbold (false); Body3.getfont (SetColor (0, 128, 2     ); Body3.getfont (). SetSize (14); 63Body3.getfont (). SetName ("Chinese Choi Wan"); Body3.setvalue ("Sockets can be categorized according to the nature of the communication, which is visible to the user.") Applications generally communicate only between sockets in the same class. However, as long as the underlying communication protocol allows, different types of sockets can still communicate. There are two different types of sockets: Stream sockets and datagram sockets. \ n "); ParagraphFormat bodyPara3 = Body3.getparagraphformat (); Bodypara3.setlinespacingrule (Wdlinespacing.wdlin     espacedouble); bodypara3.setalignment (Wdparagraphalignment.wdalignparagraphleft); 68 Bodypara3.setfirstlineindent (21); 69 70//Fourth paragraph, insert picture dataregion body4 = doc.createdataregion ("Po_body4", Datare Gioninserttype.after, "po_body3"); Body4.setvalue ("[Image]doc/logo.png[/image]");//body4.setvalue ("[Word]doc /1.doc[/word];//can also embed other Word files ParagraphFormat bodyPara4 = Body4.getparagraphformat (); Wdparagraphalignment.wdalignparagraphcenter), Poctrl1.setwriter (DOC), Poctrl1.webopen ("Doc/template.doc", O Penmodetype.docnormaledit, "Zhang San");

In the development process may encounter problems, here to give you an explanation.

DataRegion (data region): A word bookmark that begins with the data region, which is the "Po_", that marks the location where the data is inserted in the file when the file is generated.

Two special places that need not be defined: [Home]: Indicates the position of the top of the page of a Word file; [end]: Indicates the end position of the Word file;

--------------------------------------------------------------------------------------------------------------- ------------------------------------

Createdataregion
createdataregion (Java.lang.String Newdataregionname,                                   Dataregioninserttype Inserttype,                                   java.lang.String relativedataregionname)                            throws Java.lang.Exception,                                   java.io.IOException
Creates a new data region and returns the DataRegion object.

Calling this method makes it easy to create a new data region before or after the specified data region and assign or control the new data region.

Relativedataregionname is typically a pre-defined data area for a user that already exists in the document, but Relativedataregionname can also use two special data areas reserved by the Pageoffice development platform: [HOME] and [ END]. [HOME] and [end] do not need to be manually defined by the user to open the document, which represents the document's start and end cursor positions, respectively.

If the current document is a blank document and no data region exists, use [HOME] and [END] to generate an illustrated document from a blank document.

Parameters:
newDataRegionName -The name of the new dataregion. Note: You should ensure that you do not have the same name as a data region that already exists in your document.
insertType -How to insert the new dataregion.
relativeDataRegionName -the name of the relative dataregion.
Return:
returns the DataRegion object.

--------------------------------------------------------------------------------------------------------------- ------------------------------------

When a blank Word file is a template, there is no data area in the file, the following code is the Word file after the beginning of the page to create a data region Po_title, assigned the title content, and then after the title to create a data region po_body, The contents of the text are assigned:

1 dataregion title = Doc.createdataregion ("Po_title", Dataregioninserttype.after, "[Home]"); 2 Title.setvalue ("C # Middle socket multithreaded Programming instance \ n "); 3 dataregion BODY = doc.createdataregion (" Po_body ", Dataregioninserttype.after," Po_title "); 4 Body.setvalue ("Microsoft has a new language with vs.net ...");

The rest of the code should not be explained. Run the above program to generate the Word file as shown.

  

SOURCE download: http://www.zhuozhengsoft.com/dowm/download pageoffice for Java, after decompression, copy the Samples4 folder to the WebApps directory of Tomcat, Access:/http Localhost:8080/samples4/index.html

View examples: Three, 7, fully programmed to dynamically generate Word files

Reproduced Java generates Word documents

Related Article

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.