In the process of developing document systems or office systems, sometimes we need to export word documents. I found a function of generating word files with PageOffice on the Internet, and I took this out and shared it with you.
Generating a word file is essentially the same as editing a word document, except that when generating a word document with a program, it is replaced by code. The following examples mainly include adding titles, text (paragraph, font, font size, double-line spacing, alignment, first line indentation, etc.), inserting pictures, etc. The content given in the example written here is only part of the methods for generating word files by the PageOffice component. If you need to use more methods, you need to check the API according to your actual needs. API address: http://www.zhuozhengsoft.com/help/java3/index.html
View the objects and methods under the com.zhuozhengsoft.pageoffice.wordwriter package are used to generate word files
Then go directly to the code first:
1 PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request); 2 poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz" ); 3 // Create a WordDocument object 4 WordDocument doc = new WordDocument(); 5 // Set the content title 6 // Create a DataRegion object, PO_title is the automatically added bookmark name, the bookmark name needs to be prefixed with "PO_", and the bookmark name cannot be repeated 7 // The three parameters are the name of the new bookmark to be inserted and the insertion position of the new bookmark , the associated bookmark name ("[home]" represents the first position of the Word document) 8 DataRegion title = doc.createDataRegion("PO_title" , 9 DataRegionInsertType.After, "[home]" ); 10 // Assign value 11 to the DataRegion object title.setValue("Socket multi-threaded programming instance in C#\n" ); 12 // Set the font: thickness, size, font name, whether it is italic 13 title.getFont().setBold( true ); 14 title.getFont().setSize(20 ); 15 title.getFont().setName("bold" ); 16 title.getFont().setItalic( false ); 17 // Define the paragraph object 18 ParagraphFormat titlePara = title. getParagraphFormat(); 19 // Set paragraph alignment 20 titlePara.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter); 21 //Set paragraph line spacing 22 titlePara.setLineSpacingRule(WdLineSpacing.wdLineSpaceMultiple); 23 24 // Set content 25 // The first paragraph 26 // Create a DataRegion object, PO_body is the automatically added bookmark name 27 DataRegion body = doc.createDataRegion("PO_body ",DataRegionInsertType.After, "PO_title" ); 28 // Set the font: thickness, italic, size, font name, font color 29 body.getFont().setBold( false ); 30 body.getFont().setItalic ( true ); 31 body.getFont().setSize(10 ); 32 // Set the Chinese font name 33 body.getFont().setName("Kaiti" ); 34 // Set the English font name 35 body.getFont().setName("Times New Roman" ); 36 body.getFont().setColor(Color.RED); 37 // Assign a value to the DataRegion object 38 body.setValue(" is a new language introduced by Microsoft with VS.net. As an emerging language, it has the robustness of C++ and the RAD features of VB and so on. Moreover, The main purpose of Microsoft's launch of C# is to fight against Sun's Java. Everyone knows the powerful functions of the Java language, especially in network programming. Therefore, C# is naturally unwilling to lag behind others in network programming. This article will introduce C# to you. Let me introduce some basic knowledge of socket programming, in order to give you a general understanding of it. First, let me introduce the concept of sockets.\n" ); 39 // Create ParagraphFormat object 40 ParagraphFormat bodyPara = body.getParagraphFormat(); 41 // Set the line spacing, alignment, and indentation of the first line of the paragraph 42 bodyPara.setLineSpacingRule(WdLineSpacing.wdLineSpaceAtLeast); 43 bodyPara.setAlignment(WdParagraphAlignment.wdAlignParagraphLeft); 44 bodyPara.setFirstLineIndent(21); 45 46 //第二段 47 DataRegion body2 = doc.createDataRegion("PO_body2",DataRegionInsertType.After, "PO_body"); 48 body2.getFont().setBold(false); 49 body2.getFont().setSize(12); 50 body2.getFont().setName("黑体"); 51 body2.setValue("The socket is the cornerstone of communication and the basic operation unit of network communication that supports the TCP/IP protocol. The socket can be regarded as the endpoint for two-way communication between processes between different hosts, which constitutes a single host A programming interface within and between the entire network. Sockets exist in the communication domain, which is an abstract concept introduced to deal with general thread communication through sockets. Sockets are usually associated with sockets in the same domain. Sockets exchange data (data exchanges may also cross domain boundaries, but some interpreter must be executed in this case). Various processes use this same domain to communicate with each other using the Internet Protocol Suite.\n" ); 52 // body2.setValue("[image]../images/logo.jpg[/image]"); 53 ParagraphFormat bodyPara2 = body2.getParagraphFormat(); 54 bodyPara2.setLineSpacingRule(WdLineSpacing.wdLineSpace1pt5); 55 bodyPara2.setAlignment (WdParagraphAlignment.wdAlignParagraphLeft); 56 bodyPara2.setFirstLineIndent(21 ); 57 58 // The third paragraph 59 DataRegion body3 = doc.createDataRegion("PO_body3", DataRegionInsertType.After, "PO_body2" ); 60 body3.getFont().setBold( false ); 61 body3.getFont().setColor(Color.getHSBColor(0, 128, 228 )); 62 body3.getFont().setSize(14 ); 63 body3.getFont().setName("Huawen Caiyun" ); 64 body3.setValue("Sockets can be classified according to the nature of communication, which is very important for users Is visible. Applications generally only communicate between sockets of the same type. 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" ); 65 ParagraphFormat bodyPara3 = body3.getParagraphFormat(); 66 bodyPara3.setLineSpacingRule(WdLineSpacing.wdLineSpaceDouble); 67 bodyPara3.setAlignment(WdParagraphAlignment.wdAlignParagraphLeft); 68 bodyPara3.setFirstLineIndent(21 ); 69 70 // The fourth paragraph, insert picture 71 DataRegion body4 = doc.createDataRegion("PO_body4", DataRegionInsertType.After, "PO_body3" ); 72 body4 .setValue("[image]doc/logo.png[/image]" ); 73 // body4.setValue("[word]doc/1.doc[/word]"); // can also embed other Word files 74 ParagraphFormat bodyPara4 = body4.getParagraphFormat(); 75 bodyPara4.setAlignment(WdParagraphAlignment.wdAlignParagraphCenter); 76 77 poCtrl1.setWriter(doc); 78 poCtrl1.webOpen("doc/template.doc", OpenModeType.docNormalEdit,"张三");
The problems that may be encountered during the development process are explained here.
DataRegion (data region): The data region is the word bookmark starting with "PO_", which marks the position where the data is inserted in the file when the file is generated.
Two special positions that do not need to be defined: [home]: indicates the position of the header of a word file; [end]: indicates the end position of the word file;
---------------------------------------------------------------------------------------------------------------------------------------------------
createDataRegion
public DataRegion 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 a DataRegion object.
Call this method to easily create and assign or control a new data area before or after the specified data area.
RelativeDataRegionName is generally a user-predefined data area 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 when opening the document, they represent the starting cursor position and the ending cursor position of the document respectively.
If the current document is a blank document, and a data area does not exist, use [HOME] and [END] to generate a format document with pictures and texts from the blank document.
-
- parameter:
-
newDataRegionName
- The name of the new DataRegion. Note: You should make sure not to have the same name as a data area that already exists in the document. -
insertType
- Insertion method of new DataRegion. -
relativeDataRegionName
- Relative DataRegion name. - return:
- Returns a DataRegion object.
---------------------------------------------------------------------------------------------------------------------------------------------------
When a blank word file is used as a template, there is no data area in the file. The following code creates a data area PO_title after the header of the word file, assigns the title content, and then creates it after the title A data area PO_body is assigned, and the content of the body is assigned:
1 DataRegion title = doc.createDataRegion("PO_title" , DataRegionInsertType.After, "[home]" ); 2 title.setValue("Instance of Socket multithreaded programming in C#\n" ); 3 DataRegion body = doc.createDataRegion(" PO_body",DataRegionInsertType.After, "PO_title" ); 4 body.setValue("It is a new language introduced by Microsoft with VS.net...");
The rest of the code should need no explanation. Run the above program to generate the word file as shown in the figure below.
Source code download: http://www.zhuozhengsoft.com/dowm/ Download PageOffice for Java, after decompression, copy the Samples4 folder to the Webapps directory of Tomcat, visit: http://localhost:8080/Samples4/index.html
View the example: 3. 7. Completely programmed to dynamically generate Word files