Word document generation method, system and equipment facing diversified requirements and medium
By creating entity object classes and dynamic compilation technology, loosely coupling word templates and programs, the problem of poor flexibility in word document generation methods in the existing technology is solved, and dynamic generation of custom content and styles is realized to meet diverse needs and enhance the Flexibility and versatility of the generation tool.
Patent Information
- Application Number
- CN202411300581.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-18
- Publication Date
- 2025-01-14
AI Technical Summary
The existing word document generation methods are tightly coupled with applications, and have poor flexibility and cannot meet the diverse emergency guarantee needs. Especially when demand changes, it requires frequent recoding or adjustment of code, resulting in poor program flexibility.
By creating entity object classes, including data templates and data parsing rule templates, writing query statements to extract data from the database, and defining word document display styles and rendering logic, the loose coupling of word templates and programs is realized, and dynamic compilation and hot loading are used for JavaCompiler API. Technology that supports dynamic loading of custom data and styles.
It realizes the flexibility and versatility of word document generation tools, supports dynamic changes in custom content and styles, does not need to adjust program code, meets diverse needs, and enhances the flexibility and adaptability of word document generation.
Smart Images

Figure CN119312784A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer information processing, and in particular to a method, system, device and medium for generating word documents facing diversified requirements. Background Art
[0002] With the continuous advancement of information construction, all walks of life have accumulated a large amount of data. Providing high-quality services based on data quickly is an urgent problem to be solved. Word documents are an important carrier for displaying data results. They extract the required information from the database, process it, and display it in a certain format with pictures and texts.
[0003] Currently, there are three main ways to generate word documents: program coding generation, word tag replacement generation, and generation based on template engine. Program coding is mainly based on programming languages such as C++ and JAVA. By calling the word development interface, word documents are created. Commonly used technologies include Com, ActiveX, Jacob, and Java2word. In this method, the content and style of word are solidified in the program code. When the demand changes, it can only be re-encoded and developed again. It is not flexible enough and cannot meet the needs of dynamic changes. The tag replacement method uses the tag function of word to create a word template, and then the program parses the template and replaces the placeholder with the corresponding data. This method can only achieve simple updates and replacements for word, and its application is relatively limited. The template engine method is that business personnel pre-create templates based on template syntax, and then the template engine parses the template and fills in data to finally generate the corresponding word document. Compared with the tag replacement method, the template engine can independently judge the rules and loop control rules, support users to customize template styles, and is more flexible. Commonly used template engines include FreeMarker, Enjoy, poi-tl and docxtemplater, but FreeMarker and Enjoy only support XML templates, which are not intuitive and cannot achieve WYSIWYG; poi-tl and docxtemplater support word templates, which are intuitive and easy to edit, but additional code needs to be written to render data into the word template. The template is coupled with the program code. When the template changes, the code also needs to be adjusted accordingly, which makes the program less flexible and unable to meet diverse needs.
[0004] In summary, the existing word document generation method is tightly coupled with the application, has poor flexibility, and cannot meet the diverse emergency support needs. Summary of the invention
[0005] In view of this, an embodiment of the present application provides a word document generation method for diversified needs, so as to achieve loose coupling between word templates and programs and enhance the flexibility and versatility of word document generation tools.
[0006] The embodiment of the present application provides the following technical solution: a method for generating a word document for diversified needs, comprising:
[0007] Determine the content to be displayed in the word document and create an entity object class; wherein the entity object class includes a data template and a data parsing rule template;
[0008] Compile a query statement according to the entity object class, and extract relevant data from the database through the query statement according to a set query template;
[0009] The display style and rendering logic of the word document are defined, the format template is parsed and rendered for the extracted relevant data, and a word document is generated.
[0010] According to an embodiment of the present application, determining the content to be displayed in the word document and creating an entity object class includes:
[0011] According to the content to be displayed in the word document, member attributes and member methods of the entity object class are created to generate the data template;
[0012] The mapping rules between the data format of the data template and the java class file are defined to generate the data parsing rule template.
[0013] According to an embodiment of the present application, member attributes and member methods of an entity object class are created to generate a data template, including:
[0014] When creating member attributes of an entity object class, member attribute tags are designed based on XML syntax and values are assigned to the attribute tags, and information required by the entity object class is determined according to the values of the attribute tags.
[0015] According to one embodiment of the present application, the attribute label includes a single label and a label set; wherein, multiple dependent packages, or multiple member attributes, or multiple member methods, or annotations of multiple member methods of the entity object class are respectively taken as one of the label sets.
[0016] According to an embodiment of the present application, a query statement is written according to the entity object class, and relevant data is extracted from a database through the query statement according to a set query template, including:
[0017] Write query statements based on resultmap rules, design query logic and database-to-entity object mapping rules, obtain the set query template, and extract relevant data from the database through the query statement according to the set query template.
[0018] According to an embodiment of the present application, defining a word document display style and rendering logic includes:
[0019] The display style is edited on Word, and the variable names corresponding to the placeholders are matched one by one with the class names and variable names in the data template to form a format template. The format template of the extracted relevant data is parsed and rendered to generate a Word document.
[0020] According to an embodiment of the present application, it also includes:
[0021] Compiling the entity object class into bytecode using JavaCompiler API dynamic compilation technology and loading it into a Java virtual machine;
[0022] The set query template is loaded by using hot loading technology to extract relevant data from the database through the query statement.
[0023] This application also provides a word document generation system for diversified needs, including:
[0024] An entity creation module, used to determine the content to be displayed in the word document and create an entity object class; wherein the entity object class includes a data template and a data parsing rule template;
[0025] A data extraction module, used to write a query statement according to the entity object class, and extract relevant data from the database through the query statement according to a set query template;
[0026] The data rendering module is used to define the display style and rendering logic of the word document, parse and render the extracted relevant data according to the format template, and generate a word document.
[0027] The present application also provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the above-mentioned method for generating word documents for diversified needs when executing the computer program.
[0028] The present application also provides a computer-readable storage medium, which stores a computer program for executing the above-mentioned word document generation method for diversified needs.
[0029] Compared with the prior art, the beneficial effects that can be achieved by at least one of the above-mentioned technical solutions adopted in the embodiments of this specification include at least the following: the embodiments of the present invention provide a word document generation method for diversified needs, support customized word display content and style, automatically extract and process relevant data from the database, and finally generate a word document to meet diversified needs, realize loose coupling of word templates and programs, and enhance the flexibility and versatility of word document generation tools. BRIEF DESCRIPTION OF THE DRAWINGS
[0030] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0031] Figure 1 It is a flowchart of a method for generating a word document according to an embodiment of the present invention;
[0032] Figure 2 This is a schematic diagram of a word document generation framework according to an embodiment of the present invention;
[0033] Figure 3 is a schematic diagram of a word document generation system according to an embodiment of the present invention;
[0034] Figure 4 It is a structural schematic diagram of the computer device of the present invention. DETAILED DESCRIPTION
[0035] The embodiments of the present application are described in detail below with reference to the accompanying drawings.
[0036] The following describes the implementation methods of the present application through specific examples, and those skilled in the art can easily understand other advantages and effects of the present application from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. The present application can also be implemented or applied through other different specific implementation methods, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present application. It should be noted that, in the absence of conflict, the following embodiments and the features in the embodiments can be combined with each other. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in the field without making creative work belong to the scope of protection of the present application.
[0037] Since word is an important carrier for displaying data results, the existing word document generation method is tightly coupled with the application, has poor flexibility, and cannot meet the diverse emergency support needs. The embodiment of the present invention focuses on diverse needs and proposes a word document generation method for diverse needs, which separates the data extraction logic and display style from the degree code and designs data templates, rule templates, query templates, and format templates. Specifically, Figure 1 As shown, an embodiment of the present invention provides a method for generating a word document for diversified needs, including:
[0038] S101. Determine the content to be displayed in the word document and create an entity object class; wherein the entity object class includes a data template and a data parsing rule template;
[0039] S102. Write a query statement according to the entity object class, and extract relevant data from the database through the query statement according to the set query template;
[0040] S103. Define the display style and rendering logic of the word document, perform format template parsing and rendering on the extracted relevant data, and generate a word document.
[0041] In the embodiment of the present invention, a custom word document generation framework is adopted, specifically:
[0042] When a program generates a word document, it must first determine what content is displayed in the word document and create an entity object class; secondly, write a query statement based on the entity object to extract relevant data from the database; then define the display style (including font size, display color, line spacing, etc.) and rendering logic; and finally output the word document. In the existing word document generation method, the entity object class and query statement are solidified in the program code, resulting in a relatively fixed display content in word, which cannot meet the diverse needs of users. Word documents are composed of display styles and data content. The diverse needs of users are mainly manifested in two aspects: one is that the data content is different; the other is that the display style is different when the data content is the same. This requires that the entity object class, query statement and display style are flexible and editable, and the application can dynamically load the entity object class, query statement and display style.
[0043] like Figure 2 As shown, considering various needs comprehensively, the word document generation program is mainly composed of data extraction and data rendering modules. The data extraction module loads the uploaded entity object class and query template, and automatically extracts relevant data from the database. The data rendering module receives the data transmitted by the data extraction module, parses the format template and renders it to generate a word document.
[0044] In this embodiment, the data extraction module is transformed and expanded on the basis of SpringBoot, and the data rendering module is mainly implemented using docxtemplater. In actual operation, the client may not have a Java development environment, and syntax errors are prone to occur when creating entity object classes. In order to ensure that the entity object class is accurate, the Java class is decomposed into a data template and a data parsing rule template, and the elements required for the entity class are defined based on the data template. Then, the mapping rules from the data template to the Java class file are defined based on the data parsing rule template to avoid syntax errors. Based on this framework, when the demand changes, you only need to customize the data template, query template and format template without making any changes to the program code to generate any word document.
[0045] In one embodiment, the content to be displayed in the word document is determined and an entity object class is created, including: creating member attributes and member methods of the entity object class according to the content to be displayed in the word document, and generating the data template; defining a mapping rule between the data format of the data template and the java class file, and generating the data parsing rule template.
[0046] In the specific implementation, the content and generation process of the data template specifically include: Java classes are usually composed of member attributes and member methods. When creating member attributes, it is necessary to clarify the attribute type, name, access rights, annotations and other information; when creating member methods, it is necessary to clarify the method name, return value type, access rights, annotations, parameter types, parameter names and method content and other information. In addition, it is also necessary to clarify the class name, access rights, annotations, parent class name, class package name and dependent package name.
[0047] This embodiment mainly designs attribute tags based on XML syntax, and determines the information required by the entity class by assigning values to attribute tags. Considering that there may be multiple dependent packages, attributes, methods, and annotations, two types of tags are designed: single tags and tag sets. Single tags have only one value, and tag sets have multiple values. For example, a tag set <importpackages>Mark the collection of all dependent packages with <packagename>Marks the name of a dependent package. In addition, considering the encapsulation of the class, each attribute may have a set and get method. <attribute>Add the gs attribute to determine whether to add set and get methods.
[0048] As shown below, <attributes> 、 <methods>A collection of tag attributes and classes; <classname> 、 <extends>The name of the tag class and the name of the parent class, <modifier>and <annotations>Mark the access rights and annotations of classes, attributes and methods. When creating an entity object class, if the attribute tag has a value, the attribute tag exists; if it is empty, the relevant attribute tag is removed.
[0049]
[0050]
[0051] In specific implementation, the content and generation process of the data parsing rule template specifically include:
[0052] After determining the relevant information of the class, the XML data is then converted into a Java class. In order to improve the scalability of the system and avoid the conversion process being solidified in the program code, based on the template engine, using its "if logic judgment" and "<#list>< / #list> Traverse" design transformation rules.
[0053] As shown below, the bold part is the output content. For a single label, if <packagename>If it exists, replace "${packageName}" with "package name" to generate the "package package name;" statement; for the tag set, if <importpackages>Exists, use <#list>< / #list> Traverse the elements in the tag set, and then replace them with the attribute values of the tags to generate corresponding statements; in addition, when the value of gs is "all", both set and get functions are generated; when it is "getter", only the get function is generated; when it is "setter", only the set function is generated.
[0054]
[0055]
[0056]
[0057] method.throwsExceptions? ? >${method.throwsExceptions}< / #if> {
[0058] <#if method.methodBody? ? >
[0059] ${method.methodBody}
[0060] < / #if>
[0061] }
[0062] < / #list>
[0063] < / #if>
[0064] }
[0065] In one embodiment, a query statement is written according to the entity object class, and relevant data is extracted from the database through the query statement according to a set query template, including: writing a query statement based on a resultmap rule, designing query logic and a mapping rule from the database to the entity object, obtaining the set query template, and extracting relevant data from the database through the query statement according to the set query template.
[0066] In specific implementation, the content and generation process of the query template specifically include:
[0067] After completing the creation of the entity class, it is also necessary to clarify how to extract data from the database and map it to the entity class. The query template contains data query logic and mapping rules. This embodiment mainly writes query statements based on resultmap rules, designs mapping rules, and realizes the mapping from database to entity objects.
[0068] As shown below, <resultmap>Mark the data mapping rule, where id is the unique identifier of the mapping rule, type is the mapped entity object, column is the field name of the database table, and property is the attribute of the entity object. <result>Map a field in a data table to an entity attribute. Since the entity object class organizes and describes related data from the perspective of the object, and in a relational database, in order to meet the three paradigms, object data may be split into multiple tables, so there may be one-to-one or one-to-many situations when the data in the database is mapped to the entity object. When a one-to-many mapping occurs, the <collection>Tags for nested processing. <select>Used to mark data query logic, where id is the unique identifier of the query logic, and resultMap is consistent with the id in the tag.
[0069] <resultMap id="GetCountryJBQK"type="Country">
[0070]
[0071] In one embodiment, the display style and rendering logic of a word document are defined, including: editing the display style on word, making the variable name corresponding to the placeholder correspond to the class name and the variable name in the data template one by one to form a format template, parsing and rendering the format template of the extracted relevant data, and generating a word document.
[0072] In specific implementation, the format template directly affects the display style and generation effect of word. After determining the data content, the format template needs to identify the data scalar, set the display style, and control the rendering process. In view of the fact that the display format of word edited based on XML is not intuitive and WYSIWYG is not possible, this embodiment directly edits the display style on word. For example: using the placeholder {{variable name}} to represent the content to be updated, making the variable name correspond to the class name and the variable name in the data template one by one, such as the variable name in the format template corresponding to the variable gjmc is Country.gjmc. The display style of the variable name will be inherited during rendering, so that the final display effect can be visually controlled by editing the display parameters of Country.gjmc. In addition, when displayed in a table form, %tr and %tc are used for identification; when a variable corresponds to multiple data, {%for XX in BBB%}{%endfor%} is used for loop output.
[0073] In one embodiment, it also includes: using JavaCompiler API dynamic compilation technology to compile the entity object class into bytecode and load it into the Java virtual machine; using hot loading technology to load the set query template, which is used to extract relevant data from the database through the query statement.
[0074] In a specific embodiment, after importing the entity class file, the entity class file needs to be compiled into bytecode and loaded into the JVM (Java Virtual Machine) before it can be run. This embodiment mainly uses JavaCompilerAPI to dynamically compile the entity class file. First, use the getSystemJavaComp iler() method to obtain a JavaCompiler instance; then use DiagnosticCollector() to create a Diagnos ticCollector instance, and use getStandardFileManager to obtain a StandardJavaFileManager instance; finally, create a CompilationTask instance, and call the call() method to start the compilation process.
[0075] In order to ensure that the query template is effective, the hot loading technology is used to load the query template into the data extraction module. First, org.apache.ibatis.session.Configuration is obtained through sqlSessionFactory.getConfiguration(), the configuration information in Configuration is cleared, and then an XMLMapperBuilder instance is created, the parse() method is called to parse the query template, and the Configuration is loaded.
[0076] The word document generation method of this embodiment is experimentally verified. The database contains information such as the development history, natural environment, political environment, social culture, GDP growth rate and the proportion of the three major industries of Country A and Country B, which are stored in 7 data tables respectively. Taking the generation of basic information of the two countries as an example, the template-driven word document generation framework is verified. The results show that the present invention realizes the loose coupling of the word template and the program, and enhances the flexibility and versatility of the word document generation tool.
[0077] As shown in FIG3, the present application also provides a word document generation system 200 for diversified needs, including:
[0078] an entity creation module 201, used to determine the content to be displayed in the word document and create an entity object class; wherein the entity object class includes a data template and a data parsing rule template;
[0079] a data extraction module 202, used to write a query statement according to the entity object class, and extract relevant data from the database through the query statement according to the set query template;
[0080] a data rendering module 203, used to define the word document display style and rendering logic, perform format template parsing and rendering on the extracted relevant data, and generate a word document.
[0081] In specific implementation, the entity creation module 201 creates member attributes and member methods of the entity object class according to the content to be displayed in the word document, and generates the data template; defines the mapping rules between the data format of the data template and the java class file, and generates the data parsing rule template.
[0082] Wherein, when creating member attributes of an entity object class, a member attribute tag is designed based on XML syntax and a value is assigned to the attribute tag, and the information required by the entity object class is determined according to the value of the attribute tag. The attribute tag includes a single tag and a tag set; wherein, multiple dependent packages, or multiple member attributes, or multiple member methods, or annotations of multiple member methods of the entity object class are respectively regarded as a tag set.
[0083] In specific implementation, the data extraction module 202 writes a query statement based on the resultmap rule, designs the query logic and the mapping rules from the database to the entity object, obtains the set query template, and extracts relevant data from the database through the query statement according to the set query template.
[0084] In specific implementation, the data rendering module 203 edits the display style on the word, matches the variable name corresponding to the placeholder with the class name and variable name in the data template one by one, forms a format template, parses and renders the format template of the extracted relevant data, and generates a word document.
[0085] The embodiment of the present invention focuses on diversified needs, proposes a word document generation framework, separates the data extraction logic and display style from the degree code, designs a data template, a rule template, a query template and a format template, and finally verifies it by taking the generation of basic information of country A and country B as an example. The experimental results show that the method is feasible, realizes the loose coupling between the word template and the program, enhances the flexibility and versatility of the word document generation tool, and can meet the diversified emergency support needs to a certain extent.
[0086] In one embodiment, a computer device is provided, as shown in FIG4, including a memory, a processor and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the above-mentioned data storage method for data sets and / or the above-mentioned word document generation method for diversified needs are implemented.
[0087] Specifically, the computer device may be a computer terminal, a server or a similar computing device.
[0088] In the present embodiment, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program for executing any of the above-mentioned data storage methods for data sets and / or the above-mentioned word document generation methods for diversified needs.
[0089] Specifically, the computer-readable storage medium includes permanent and non-permanent, removable and non-removable media, and information storage may be implemented by any method or technology. The information may be a computer-readable instruction, a data structure, a program module or other data. Examples of computer-readable storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassette, magnetic tape disk storage or other magnetic storage device or any other non-transmission medium, which may be used to store information that can be accessed by a computing device. According to the definition in this article, computer-readable storage media does not include temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0090] Obviously, those skilled in the art should understand that the modules or steps of the above-mentioned embodiments of the present invention can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, optionally, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order from that here, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. In this way, the embodiments of the present invention are not limited to any specific combination of hardware and software.
[0091] The above description is only a specific implementation method of the present application, but the protection scope of the present application is not limited thereto, and any changes or replacements that can be easily thought of by any technician familiar with the technical field within the technical scope disclosed in the present application should be covered within the protection scope of the present application. Therefore, the protection scope of the present application shall be based on the protection scope of the claims.< / select> < / collection> < / result> < / resultmap> < / importpackages> < / packagename> < / annotations> < / modifier> < / extends> < / classname> < / methods> < / attributes> < / attribute> < / packagename> < / importpackages>
Claims
1. A word document generation method for diversified needs, characterized in that: include: Determine the content to be displayed in the word document and create an entity object class; wherein the entity object class includes a data template and a data parsing rule template; Compile a query statement according to the entity object class, and extract relevant data from the database through the query statement according to a set query template; The display style and rendering logic of the word document are defined, the format template is parsed and rendered for the extracted relevant data, and a word document is generated.
2. The method for generating a word document for diversified needs according to claim 1, characterized in that: Determine the content to be displayed in the word document and create an entity object class, including: According to the content to be displayed in the word document, member attributes and member methods of the entity object class are created to generate the data template; The mapping rules between the data format of the data template and the java class file are defined to generate the data parsing rule template.
3. The method for generating a word document for diversified needs according to claim 2, characterized in that: Create member attributes and member methods of entity object classes and generate data templates, including: When creating member attributes of an entity object class, member attribute tags are designed based on XML syntax and values are assigned to the attribute tags, and information required by the entity object class is determined according to the values of the attribute tags.
4. The method for generating a word document for diversified needs according to claim 2, characterized in that: The attribute tags include single tags and tag sets; wherein, multiple dependent packages, or multiple member attributes, or multiple member methods, or multiple member method annotations of the entity object class are respectively used as a tag set.
5. The method for generating word documents for diversified needs according to claim 1, characterized in that: Writing a query statement according to the entity object class, and extracting relevant data from the database through the query statement according to the set query template, including: Write query statements based on resultmap rules, design query logic and database-to-entity object mapping rules, obtain the set query template, and extract relevant data from the database through the query statement according to the set query template.
6. The method for generating a word document for diversified needs according to claim 1, characterized in that: Define the word document display style and rendering logic, including: The display style is edited on Word, and the variable names corresponding to the placeholders are matched one by one with the class names and variable names in the data template to form a format template. The format template of the extracted relevant data is parsed and rendered to generate a Word document.
7. The method for generating a word document for diversified needs according to claim 1, characterized in that: Also includes: Compiling the entity object class into bytecode using JavaCompiler API dynamic compilation technology and loading it into a Java virtual machine; The set query template is loaded by using hot loading technology to extract relevant data from the database through the query statement.
8. A word document generation system for diversified needs, characterized in that: include: An entity creation module, used to determine the content to be displayed in the word document and create an entity object class; wherein the entity object class includes a data template and a data parsing rule template; A data extraction module, used to write a query statement according to the entity object class, and extract relevant data from the database through the query statement according to a set query template; The data rendering module is used to define the display style and rendering logic of the word document, parse and render the extracted relevant data according to the format template, and generate a word document.
9. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the method for generating a word document for diversified needs according to any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program for executing the method for generating a word document for diversified needs as described in any one of claims 1 to 7.