Implementation method for generating word document export according to template

By using the Docxtemplater library to process Word template files in the Node.js environment, the format setting problem during Word document generation in the existing technology is solved, and dynamic data is filled and exported, which is suitable for different browsers and Node.js environments.

CN119538898APending Publication Date: 2025-02-28SHANGHAI INST OF PROCESS AUTOMATION & INSTR
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411746474.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-02
Publication Date
2025-02-28

AI Technical Summary

Technical Problem

The existing technology has formatting problems when generating Word documents, such as the text in the table cannot be set line spacing or first line indentation, and the EasyPoi library has problems when setting text in the font in the table.

Method used

Using the Docxtemplater library, by installing related dependencies, create Word template files, read template files, process data and pictures in the Node.js environment, and fill dynamic data into .docx templates and export them into Word documents.

Benefits of technology

It implements the filling of dynamic data into .docx templates in Node.js environment and exporting them as Word documents, solves the formatting problem, and supports use in different browsers and Node.js environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119538898A_ABST
    Figure CN119538898A_ABST
Patent Text Reader

Abstract

The invention provides an implementation method for generating word document export according to a template, and relates to the technical field of code generation. The method comprises the following steps of: installing related dependencies used by a Docxtemplater; the method comprises the following steps of: creating a word template file according to a Docxtemplater; and packaging the Js and calling an export method. In a Node.js environment, by reading a template file and processing data and pictures, the dynamic data is filled into a. Docx template and exported as a Word document, and the data and the pictures in a JSON format are embedded into the generated document and exported. The Docxtemplater plug-in is good in flexibility, capable of processing complex document structures and inserting various types of data, expandability is achieved, a user-defined processor can be added, compatibility is good, and the Docxtemplater plug-in can be used in different browsers and Node.js environments.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code generation, and in particular to a method for realizing exporting a Word document generated according to a template. Background Art

[0002] The front-end method of generating Word documents based on templates includes Apache POI library and EasyPoi library. ApachePOI library is a Java library for processing Microsoft Office documents, including Word, Excel, etc. When using ApachePOI to generate Word documents, you first need to import related dependencies, such as poi and poi-ooxml. Then, write code to implement the function of generating new Word files based on Word templates. Apache POI provides a more flexible operation method, but it will also encounter some formatting problems, such as the inability to set line spacing or first line indentation for text in tables. EasyPoi library is a simplified version of the tool based on Apache POI, mainly used to simplify the operation of Excel and Word. When using EasyPoi to generate Word documents, although the operation is relatively simple, there are also some problems, such as the inability to set Chinese fonts in tables, the line spacing and first line indentation problems are solved, etc. In addition, when using EasyPoi, you also need to pay attention to some problems when reading template files. Summary of the invention

[0003] The purpose of the present invention is to provide a method for generating a word document export based on a template to solve the problem of generating a word document export in view of the above-mentioned deficiencies in the prior art.

[0004] To achieve the above purpose, the technical solution adopted by the present invention is as follows:

[0005] The present invention provides a method for generating a word document export according to a template, the method comprising:

[0006] Step S1, install the related dependencies used by Docxtemplater, including docxtemplater, jszip, jszip-utils, pizzip, and file-saver plug-ins;

[0007] Step S2, creating a word template file according to Docxtemplater;

[0008] Step S3: Encapsulate Js and call the export method.

[0009] Optionally, in step S2, vue cli3 / vue cli4 stores the word template test.docx in the public file, and vue cli2 stores the word template test.docx in the static file.

[0010] Optionally, in step S3, create an exportWordDocx.js file, define an exportWordDocx function, receive three input parameters demoUrl, docxData and fileName, where demoUrl represents the exported word document template path, docxData represents the dept and applyDate fields defined in the template document integrated into docxData, and fileName represents the exported file name.

[0011] Optionally, in step S3, demoUrl is passed to the JSZipUtils.getBinaryContent method to read the binary content of the template file, and then a PizZip instance is created with the content of the template, and then a docxtemplater instance object is created and loaded.

[0012] Optionally, use the doc.setData method to set the value of a template variable, with the object's keys matching the template variable names.

[0013] The beneficial effects of the present invention include:

[0014] The implementation method for generating word document export according to template provided by the present invention includes: step S1, installing related dependencies used by Docxtemplater, and the related dependencies include docxtemplater, jszip, jszip-utils, pizzip, and file-saver plug-ins; step S2, creating a word template file according to Docxtemplater; step S3, encapsulating Js and calling the export method. In the Node.js environment, by reading the template file, processing data and pictures, dynamic data is filled into the .docx template and exported as a Word document, and data and pictures in JSON format are embedded into the generated document and exported. The Docxtemplater plug-in has good flexibility, can handle complex document structures, insert various types of data, has scalability, can add custom processors, has good compatibility, and can be used in different browsers and Node.js environments. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0016] Figure 1 The present invention provides a flow chart of a method for generating a word document export based on a template according to an embodiment of the present invention. DETAILED DESCRIPTION

[0017] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0018] The Docxtemplater library is a template engine for generating and filling Microsoft Word documents. It automatically creates documents with dynamic content by combining templates with data. Docxtemplater is based on JavaScript and uses templates and data as input. It automatically generates documents with dynamic content based on the placeholders in the template and the values ​​in the data. For those who need to highly customize the document content and style, Docxtemplater is more suitable for front-end development.

[0019] Figure 1 The present invention provides a flow chart of a method for generating a word document export based on a template according to an embodiment of the present invention.

[0020] The method provided by the invention uses Docxtemplater to generate a word document according to a template.

[0021] like Figure 1 As shown, the implementation method for generating a word document export according to a template provided by the present invention includes:

[0022] Step S1: Install the dependencies used by Docxtemplater.

[0023] Related dependencies include docxtemplater, jszip, jszip-utils, pizzip, and file-saver plug-ins.

[0024] npminstall docxtemplater pizzip --save;

[0025] npminstall jszip-utils–save;

[0026] npminstall jszip –save;

[0027] npminstall file-saver–save.

[0028] Step S2: Create a word template file based on Docxtemplater.

[0029] In step S2, create a word template: public / test.docx. vue cli3 / vue cli4 stores the word template test.docx in the public file, and vue cli2 stores the word template test.docx in the static file. Docxtemplater syntax: {%img} picture, {#list}{ / list} loop, if judgment, {#list}{ / list}{^list}{ / list}if else and {str} text.

[0030] Step S3: Encapsulate Js and call the export method.

[0031] In step S3, create an exportWordDocx.js file and define an exportWordDocx function that receives three input parameters: demoUrl, docxData, and fileName. demoUrl represents the exported word document template path, docxData represents the dept and applyDate fields defined in the template document and is integrated into docxData. FileName represents the exported file name to facilitate renaming and other operations.

[0032] In step S3, pass demoUrl to JSZipUtils.getBinaryContent method to read the binary content of the template file, then create a PizZip instance with the content of the template, and then create and load the docxtemplater instance object. Use doc.setData method to set the value of the template variable, and the key of the object is consistent with the variable name on the template, and the value is the value to be placed on the template.

[0033] It should be noted that if the value defined in the template is null or undefined, the corresponding place in the exported Word document will directly display undefined. The solution is to return the nullGetter value in the doc.setOptions method and set it to null. Finally, export the Word document through the saveAs method.

[0034] In summary, in the Node.js environment, by reading template files, processing data and images, dynamic data can be filled into the .docx template and exported as a Word document, and data and images in JSON format can be embedded into the generated document and exported. The Docxtemplater plug-in is more flexible, can handle complex document structures, insert multiple types of data, and has scalability, such as the ability to add custom processors, good compatibility, and can be used in different browsers and Node.js environments.

[0035] The above embodiments are only for illustrating the technical concept and features of the present invention, and their purpose is to enable ordinary technicians in the field to understand the content of the present invention and implement it. They cannot be used to limit the protection scope of the present invention. Any equivalent changes or modifications made according to the spirit of the present invention should be included in the protection scope of the present invention.

Claims

1. A method for generating a word document export based on a template, characterized in that: The method comprises: Step S1, installing the related dependencies used by Docxtemplater, the related dependencies include docxtemplater, jszip, jszip-utils, pizzip, and file-saver plug-ins; Step S2, creating a word template file according to Docxtemplater; Step S3: Encapsulate Js and call the export method.

2. The method for generating a word document export according to a template according to claim 1, characterized in that: In step S2, vue cli3 / vue cli4 stores the word template test.docx in the public file, and vue cli2 stores the word template test.docx in the static file.

3. The method for generating a word document export according to a template according to claim 1, characterized in that: In the step S3, an exportWordDocx.js file is created, and an exportWordDocx function is defined to receive three input parameters: demoUrl, docxData, and fileName, where demoUrl represents the exported word document template path, docxData represents the integration of the dept and applyDate fields defined in the template document into docxData, and fileName represents the exported file name.

4. The method for generating a word document export according to a template according to claim 3, characterized in that: In step S3, the demoUrl is passed to the JSZipUtils.getBinaryContent method to read the binary content of the template file, and then a PizZip instance is created with the content of the template, and then a docxtemplater instance object is created and loaded.

5. The method for implementing exporting a word document generated according to a template according to claim 4 is characterized in that: Use the doc.setData method to set the value of the template variable, and the key of the object is consistent with the variable name on the template.