CN115983230A - Word document generation method and device based on JSON data - Google Patents

Word document generation method and device based on JSON data Download PDF

Info

Publication number
CN115983230A
CN115983230A CN202211712932.XA CN202211712932A CN115983230A CN 115983230 A CN115983230 A CN 115983230A CN 202211712932 A CN202211712932 A CN 202211712932A CN 115983230 A CN115983230 A CN 115983230A
Authority
CN
China
Prior art keywords
file
generating
generate
instruction
word
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202211712932.XA
Other languages
Chinese (zh)
Inventor
赵青杰
曹浪
孙昊翔
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou DPTech Technologies Co Ltd
Original Assignee
Hangzhou DPTech Technologies Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Hangzhou DPTech Technologies Co Ltd filed Critical Hangzhou DPTech Technologies Co Ltd
Priority to CN202211712932.XA priority Critical patent/CN115983230A/en
Publication of CN115983230A publication Critical patent/CN115983230A/en
Pending legal-status Critical Current

Links

Images

Landscapes

  • Document Processing Apparatus (AREA)

Abstract

The application relates to a word document generation method and device based on JSON data, electronic equipment and a computer readable medium. The method comprises the following steps: generating a plurality of bottom layer data modules corresponding to chapters, tables and diagrams based on JSON data; extracting the public functions and the association relation to generate a word style template; acquiring real-time operation of a user on line and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; and compressing the XML format file to convert the XML format file into a word document displayed in real time. The word document generation method, device, electronic equipment and computer readable medium based on JSON data can solve the problem that the prior art strongly depends on the operation environment, solve the problems that the development process is complicated and the error probability is high during program operation in the prior art, and solve the problem that modification is inconvenient when the requirement changes in a template generation mode.

Description

Word document generation method and device based on JSON data
Technical Field
The disclosure relates to the field of computer information processing, in particular to a method and a device for generating a word document based on JSON data, electronic equipment and a computer readable medium.
Background
In a system such as a related result analysis, a result presentation document is a very important output file. Because the result display document is directly presented to the user, the display effect of the document is very important, and the document display content is intuitive and highly readable. Users generally use such documents as summary or report, and therefore, the documents are easy to operate and process.
The conventional result documents are generally HTML, EXCEL and Word documents. The HTML page has beautiful presentation effect and style but the file cannot be operated and modified; EXCEL is dominant in displaying a large number of data tables, but the statistical analysis text information cannot be reasonably displayed; the Word document can display rich pictures and texts, and a user can easily operate and modify the Word document, so that partial advantages of HTML and EXCEL are combined, the Word document is a better presentation mode, and the requirement for displaying contents by the Word document is more and more urgent.
Therefore, in order to solve the problem that the result information is displayed in the Word document, some proper technical methods are needed to convert the result information data into the Word document. However, in various word document generation modes in the prior art, developers are required to spend a great deal of time and energy from testing to version release.
Therefore, a new method, device, electronic device and computer readable medium for generating word documents based on JSON data are needed.
The above information disclosed in this background section is only for enhancement of understanding of the background of the application and therefore it may contain information that does not constitute prior art that is already known to a person of ordinary skill in the art.
Disclosure of Invention
In view of this, the present application provides a word document generation method, device, electronic device and computer readable medium based on JSON data, which can solve the problem in the prior art that there is a strong dependence on the operating environment, solve the problems in the prior art that the development process is tedious and the error probability is large during program operation, and solve the problem that modification is inconvenient when the demand changes in the template generation manner.
Other features and advantages of the present application will be apparent from the following detailed description, or may be learned by practice of the application.
According to one aspect of the application, a word document generation method based on JSON data is provided, and the method comprises the following steps: generating a plurality of bottom layer data modules corresponding to chapters, tables and diagrams based on JSON data; extracting the public function and the incidence relation to generate a word style template; acquiring real-time operation of a user on line, and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; and compressing the XML format file to convert the XML format file into a word document displayed in real time.
In an exemplary embodiment of the present application, generating a plurality of underlying data modules corresponding to chapters, tables, charts based on JSON data includes: generating a chapter bottom layer data module, a secondary catalog bottom layer data module and a title bottom layer data module step by step according to the chapter title level; defining a table name and generating a bottom layer data module corresponding to the table name; and defining the icon name and generating a bottom layer data module corresponding to the icon name.
In an exemplary embodiment of the present application, extracting common functions and associations to generate a word style template includes: extracting files according to the format of the word document to be generated to form a structural relationship; extracting a public function corresponding to the XML format file according to the file composition structure relationship; and extracting the association relation between the XML format file and other documents according to the file composition structure relation.
In an exemplary embodiment of the present application, acquiring a real-time operation of a user on line, and generating an operation instruction includes: and generating the operation instruction according to a real-time input command of a user on line, wherein the operation instruction is a JavaScript script language.
In an exemplary embodiment of the present application, loading the underlying data module according to the operation instruction to generate an XML format file includes: when the operation instruction is a keyword instruction, extracting < w: t > < w: r > nodes from the XML format file; configuring a < w: rPr > </w: rPr > style of a < w: t > < w: r > node according to the generated key instruction.
In an exemplary embodiment of the present application, loading the underlying data module according to the operation instruction to generate an XML format file includes: when the operation instruction is a generation paragraph instruction, extracting nodes wrapping the (w: p > ... w: p >) in an XML format file; and (3) packaging < w: p > \8230andthe < w: rPr > style of the node of the < w: p > according to the generation paragraph instruction configuration.
In an exemplary embodiment of the present application, loading the underlying data module according to the operation instruction to generate an XML format file includes: when the operation instruction is a generate table instruction, identifying a table by < w: tbl > </w: tbl > based on the generate table instruction; and/or identifying a form style by < w: tblPr > </w: tblPr > based on the generate form instruction, and/or identifying a line by < w: tc > </w: tc > based on the generate form instruction; and/or performing cell generation through vMerge based on the table generation instruction.
In an exemplary embodiment of the present application, loading the underlying data module according to the operation instruction to generate an XML format file includes: when the operation instruction is a chart instruction, acquiring a relationship between a chart file and an excel file corresponding to the chart; compressing and placing excel files corresponding to the graph into a first preset position; putting the chart corresponding chart into a second preset position; the relationship between the charts file and the xlsx file is added in the file composition structure relationship.
In an exemplary embodiment of the present application, loading the underlying data module according to the operation instruction to generate an XML format file includes: when the operation instruction generates a picture instruction, converting the picture into a base64 format; and adding the incidence relation between the picture and the xml file in the file composition structure relation.
In an exemplary embodiment of the present application, compressing the XML format file to convert the XML format file into a word document to be displayed in real time includes: compressing the XML format file by a JSZip method; growing a zip file; and converting the zip file into a word document displayed in real time.
According to an aspect of the present application, a word document generating apparatus based on JSON data is provided, the apparatus including: the data module is used for generating a plurality of bottom layer data modules corresponding to chapters, tables and diagrams based on JSON data; the template module is used for extracting the public functions and the incidence relation to generate a word style template; the instruction module is used for acquiring real-time operation of a user on line and generating an operation instruction; the loading module is used for loading the bottom layer data module according to the operation instruction to generate an XML format file; and the compression module is used for compressing the XML format file to convert the XML format file into a word document displayed in real time.
According to another aspect of the present disclosure, there is provided a method for generating a Word document based on JSON data, comprising: processing and coding data information required by Word document generation to generate a plurality of bottom layer data modules of chapters, tables and charts in a JSON data format; extracting a public function as a Word document assembly element component, and configuring a document incidence relation to build and generate Word basic setting; acquiring command operation of a user and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; and compressing the XML format file to convert the XML format file into a Word document displayed in real time.
According to an aspect of the present application, an electronic device is provided, the electronic device including: one or more processors; storage means for storing one or more programs; when executed by one or more processors, cause the one or more processors to implement a method as above.
According to an aspect of the application, a computer-readable medium is proposed, on which a computer program is stored which, when being executed by a processor, carries out the method as above.
According to the word document generation method, device, electronic equipment and computer readable medium based on JSON data, a plurality of bottom layer data modules corresponding to chapters, tables and charts are generated based on the JSON data; extracting the public functions and the association relation to generate a word style template; acquiring real-time operation of a user on line and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; the XML format file is compressed to be converted into the word file displayed in real time, the problem that the prior art strongly depends on the operation environment can be solved, the problems that the development process is complicated and the error probability is high during program operation in the prior art are solved, and the problem that modification is inconvenient when the requirement changes in a template generation mode is solved.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the application.
Drawings
The above and other objects, features and advantages of the present application will become more apparent by describing in detail exemplary embodiments thereof with reference to the attached drawings. The drawings described below are only some embodiments of the present application, and other drawings may be derived from those drawings by those skilled in the art without inventive effort.
FIG. 1 is a system block diagram illustrating a method and apparatus for generating a word document based on JSON data according to an exemplary embodiment.
FIG. 2 is a flowchart illustrating a method for word document generation based on JSON data in accordance with an exemplary embodiment.
FIG. 3 is a flowchart illustrating a method for word document generation based on JSON data in accordance with another exemplary embodiment.
FIG. 4 is a schematic diagram illustrating a method for generating a word document based on JSON data according to another exemplary embodiment.
FIG. 5 is a block diagram illustrating a JSON data based word document generating apparatus in accordance with an exemplary embodiment.
FIG. 6 is a block diagram illustrating an electronic device in accordance with an example embodiment.
FIG. 7 is a block diagram illustrating a computer-readable medium in accordance with an example embodiment.
Detailed Description
Example embodiments will now be described more fully with reference to the accompanying drawings. Example embodiments may, however, be embodied in many different forms and should not be construed as limited to the embodiments set forth herein; rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the concept of example embodiments to those skilled in the art. The same reference numerals denote the same or similar parts in the drawings, and thus, a repetitive description thereof will be omitted.
Furthermore, the described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments. In the following description, numerous specific details are provided to give a thorough understanding of embodiments of the application. One skilled in the relevant art will recognize, however, that the subject matter of the present application can be practiced without one or more of the specific details, or with other methods, components, devices, steps, and so forth. In other instances, well-known methods, devices, implementations, or operations have not been shown or described in detail to avoid obscuring aspects of the application.
The block diagrams shown in the figures are functional entities only and do not necessarily correspond to physically separate entities. I.e. these functional entities may be implemented in the form of software, or in one or more hardware modules or integrated circuits, or in different networks and/or processor means and/or microcontroller means.
The flow charts shown in the drawings are merely illustrative and do not necessarily include all of the contents and operations/steps, nor do they necessarily have to be performed in the order described. For example, some operations/steps may be decomposed, and some operations/steps may be combined or partially combined, so that the actual execution sequence may be changed according to the actual situation.
It will be understood that, although the terms first, second, third, etc. may be used herein to describe various components, these components should not be limited by these terms. These terms are used to distinguish one element from another. Thus, a first component discussed below may be termed a second component without departing from the teachings of the present concepts. As used herein, the term "and/or" includes any and all combinations of one or more of the associated listed items.
It should be understood by those skilled in the art that the drawings are merely schematic representations of exemplary embodiments, and that the blocks or flowchart illustrations in the drawings are not necessarily required to practice the present application and, therefore, should not be considered to limit the scope of the present application.
The applicant finds that at present, there are two ways to generate Word, one is that a developer uses apache POI to obtain business data, and generates Word document according to API provided by POI. Apache POI is an open source code function library, provides API for Java program to read and write format document, and generates and modifies WORD document through service system. In addition, the Apache POI is improved, a third-party plug-in freemarker is introduced to create a Word template, the Word template is converted into a template of the freemarker, and then a Word file is generated. freemarker template engine, which is a programmer-oriented middleware, is a Java class library that is a generic tool for generating output text based on templates and data to be changed. Compared with a POI (point of interest) generation mode, the mode improves the document generation efficiency, but with the complexity of a business scene, the originally generated template may need to be changed along with business adjustment, the template of a fixed telephone is not rigid enough, the setting of the one-to-one mapping relationship between the placeholder and the filling data may need to adjust the whole Word template, for example, to adjust the information of a certain section or a table, and a developer needs to spend a large amount of time and energy from testing to version release.
At present, in the prior art, a technology for generating a Word file by using a java language and a freemarker template engine exists. The technology for driving the Word template to generate the Word file by using the java class library of freemarker comprises the following steps
1. A Word style template is created.
2. And converting the Word style into an extensible markup language format file.
3. And converting the extensible language format file into a freemarker file.
4. A freemarker template is generated based on the freemarker file.
5. Outputting the freemarker template as a Word file.
The prior art has the disadvantages in the following aspects:
1. the prior art depends on a Java class library, has strong dependence on the running environment and cannot break away from Java work.
2. In the prior art, the development process is complicated, two times of conversion of a Word template and a freemarker template are involved, and the error probability is high during program operation.
3. In the prior art, a template is adopted, data and placeholders of the template are in mapping replacement relationship, and modification is inconvenient in the ever-changing requirement.
In view of various defects in the prior art, the applicant of the present application proposes a Word document generation method based on JSON data, which generates a Word document by constructing underlying XML and compressing, calls a corresponding interface according to driving data to load and assemble the XML document to generate an XML file by using a single JavaScript script language, and finally generates a Zip file of Docx. The problem that the prior art strongly depends on the operation environment can be solved. The problems that in the prior art, the development process is complex, and the error probability is high during program operation can be solved. The problem that in the prior art, a template, data and placeholders are replaced in a mapping mode, and modification is inconvenient in the continuously changing requirement can be solved. The content of the present application is explained in detail below with the aid of specific examples.
FIG. 1 is a system block diagram illustrating a method, apparatus, electronic device and computer readable medium for JSON data based word document generation according to an example embodiment.
As shown in fig. 1, the system architecture 10 may include terminal devices 101, 102, 103, a network 104, and a server 105. The network 104 serves as a medium for providing communication links between the terminal devices 101, 102, 103 and the server 105. Network 104 may include various connection types, such as wired, wireless communication links, or fiber optic cables, to name a few.
The user may use the terminal devices 101, 102, 103 to interact with the server 105 via the network 104 to receive or send messages or the like. The terminal devices 101, 102, 103 may have various communication client applications installed thereon, such as a document editing application, a web browser application, a data presentation application, an instant messaging tool, a mailbox client, social platform software, and the like.
The terminal devices 101, 102, 103 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and the like.
The terminal devices 101, 102, 103 can generate a plurality of underlying data modules corresponding to chapters, tables, charts, for example, based on JSON data; the terminal devices 101, 102, 103 may, for example, extract common functions and associations to generate word style templates; the terminal devices 101, 102, 103 may, for example, obtain real-time operations of the user and generate operation instructions; the terminal devices 101, 102, 103 may load the underlying data module according to the operation instruction to generate an XML format file, for example; the terminal devices 101, 102, 103 may, for example, compress the XML-formatted file to convert the XML-formatted file into a word document that is presented in real time.
The server 105 may be a server that provides various services, such as a background management server that supports data presentation-like websites browsed by users using the terminal devices 101, 102, 103. The background management server may perform processing such as analysis on the received various data, and display a processing result (e.g., a word analysis document) to the terminal device.
Server 105 can generate a plurality of underlying data modules corresponding to chapters, tables, charts, e.g., based on JSON data; the server 105 may, for example, extract public functions and associations to generate a word style template; the server 105 can acquire real-time operation of the user on line, for example, by the terminal devices 101, 102, 103, and generate an operation instruction; the server 105 may load the underlying data modules, for example, according to the operation instructions to generate an XML format file; the server 105 may, for example, compress the XML-formatted file to convert to a word document that is exposed in real-time.
The server 105 may be an entity server, or may be composed of multiple servers, for example, it should be noted that the method for generating a word document based on JSON data provided in the embodiment of the present application may be executed by the server 105, and accordingly, a device for generating a word document based on JSON data may be disposed in the server 105.
FIG. 2 is a flowchart illustrating a method for word document generation based on JSON data in accordance with an exemplary embodiment. The word document generating method 20 based on JSON data includes at least steps S202 to S210.
As shown in fig. 2, in S202, a plurality of underlying data modules corresponding to chapters, tables, and charts are generated based on JSON data. The chapter bottom layer data module, the secondary catalog bottom layer data module and the title bottom layer data module can be generated step by step according to the chapter title level; the table name can be defined and a bottom layer data module corresponding to the table name can be generated; icon names can also be defined and underlying data modules corresponding to the icon names can be generated.
In S204, the common functions and the association relations are extracted to generate a word style template. Extracting files according to the format of the word document to be generated to form a structural relationship; extracting a public function corresponding to the XML format file according to the file composition structure relationship; and extracting the association relationship between the XML format file and other documents according to the file composition structure relationship.
In S206, the real-time operation of the user is acquired, and an operation instruction is generated. The operation instruction can be generated according to a real-time input command of a user, and the operation instruction can be a JavaScript scripting language. The operation instruction can also be in other operation languages, and the application is not limited to the operation language.
In S208, the underlying data module is loaded according to the operation instruction to generate an XML format file.
In one embodiment, when the operation instruction is a generate key instruction, extracting < w: t > < w: r > nodes in an XML format file; configuring a < w: rPr > </w: rPr > style of a < w: t > < w: r > node according to the generated key instruction.
In one embodiment, when the operation instruction is a paragraph generation instruction, nodes wrapping < w: p > ... < w: p > are extracted from an XML format file; and (3) packaging < w: p > \8230andthe < w: rPr > style of the node of the < w: p > according to the generation paragraph instruction configuration.
In one embodiment, when the operation instruction is a generate table instruction, a table is identified by < w: tbl > </w: tbl > based on the generate table instruction; and/or identifying a form style by < w: tblPr > </w: tblPr > based on the generate form instruction, and/or identifying a line by < w: tc > </w: tc > based on the generate form instruction; and/or performing cell generation through vMerge based on the table generation instruction.
In one embodiment, when the operation instruction is a chart instruction, acquiring a relationship between a chart file and an excel file corresponding to a chart; compressing and placing excel files corresponding to the graph into a first preset position; putting the chart corresponding chart into a second preset position; the relationship between the charts file and the xlsx file is added in the file composition structure relationship.
In one embodiment, when the operation instruction generates a picture generation instruction, converting the picture into a base64 format; and adding the incidence relation between the picture and the xml file in the file composition structure relation.
In S210, the XML format file is compressed to be converted into a word document displayed in real time. The XML format file can be compressed by a JSZip method; growing a zip file; and converting the zip file into a word document displayed in real time.
More specifically, the conversion of XML into Zip and then into Docx documents can be accomplished by a compression method using JSZip files. JSZip is a tool for creating, reading and editing Zip by a JavaScript class library, wherein a generated file set is compressed into Zip. The generated Zip is the content of the file to be converted into Docx.
According to the word document generation method based on JSON data, a plurality of bottom layer data modules corresponding to chapters, tables and charts are generated based on the JSON data; extracting the public functions and the association relation to generate a word style template; acquiring real-time operation of a user on line and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; the XML format file is compressed to be converted into the word file displayed in real time, the problem that the prior art strongly depends on the operation environment can be solved, the problems that the development process is complicated and the error probability is high during program operation in the prior art are solved, and the problem that modification is inconvenient when the requirement changes in a template generation mode is solved.
It should be clearly understood that this application describes how to make and use particular examples, but the principles of this application are not limited to any of the details of these examples. Rather, these principles can be applied to many other embodiments based on the teachings of the present disclosure.
FIG. 3 is a flowchart illustrating a method for JSON data based word document generation in accordance with another exemplary embodiment. The flow 30 shown in fig. 3 is a detailed description of S202 "generating a plurality of underlying data modules corresponding to chapters, tables, and charts based on JSON data" in the flow shown in fig. 2.
As shown in fig. 3, in S302, a chapter bottom data module, a secondary directory bottom data module, and a title bottom data module are generated step by step according to the chapter title level. In order to perform corresponding assembly mapping on loaded data, defined as a set of JSON object data, a specification format needs to be designed for the data to be loaded so that the data is easy to read and convenient to locate. The data specification can be divided based on the outline and the title, and the modules are respectively divided step by step according to chapter title levels.
In one embodiment, the JSON format chapters can be divided, and the JSON data of the multi-module can be divided into multiple chapters, such as chapter one can be named section1. The small modules may be divided according to a chapter level directory, chapter 1.1, chapter 1.2 named s1_1, s1 \, 2, and so on. The chapter has no second-level directory division and is directly regarded as the chapter front part named as s1_0.
In S304, a table name is defined and a corresponding underlying data module is generated. A 5 table name, chapter name + table identification '_ t', may be defined, for example the first table name of chapter 1.1, s1_1 \/u t1.
In S306, an icon name is defined and a corresponding underlying data module is generated. A chart name may be defined, chapter name + chart identification '_ e', e.g., the first chart designation of chapter 1.1, s1_1 \ e1.
In one embodiment, common functions can be extracted, common styles can be prepared, the association 0 relation of each file can be managed, and Word style templates can be determined. DOCX is essentially a compressed zip set of multiple files, whole
The bulk structure is shown in fig. 4. The schematic diagram shown in fig. 4 is a detailed description of S204 "extracting common functions and associations to generate a word style template" in the flow shown in fig. 2.
Generating Word is to compress the series of files according to the structure in fig. 4, wherein document
The document is a core file showing data loaded into the document, and other files can be extracted from existing 5-generated Word files or generated by using POI.
Based on the structure in fig. 4, it can be known that generating Word is mainly an assembly and combination of document. Generating keywords, generating paragraphs, generating tables, generating charts and generating pictures.
0 1. Generating a keyword, wherein the keyword is wrapped in the < w: t > < w: r >. The < w: r > </w: t > node, and generating the keyword
The key is to generate the node of < w: t > < w: r >, and then configure the related < w: rPr > </w: rPr > pattern
2. A paragraph is generated, the paragraph is wrapped in the < w: p >. The < w: p > node and can contain a plurality of keywords, and the < w: pPr > </w: pPr > -can be configured as well
3. Generate the relatively complex points of the form, < w: tbl > </w: tbl > identifies the form, < w: tblPr > </w: tblPr >5 identifies the form style,
lines are identified by < w: tc > </w: tc >, and cell merging can be performed
4. Generating the chart requires inserting the generated chart file and the corresponding excel file, and introducing the relationship between the generated chart file and the corresponding excel file. Using the exportExcel to generate an excel file and compress the excel file into a word/embeddings folder,
and inserting the corresponding charts file into a Word/charts folder, and finally adding the 0 relationship between charts and xlsx.
5. Only base64 data can be added in the generated picture, the picture is converted into the base64 data, and then the association between the picture and the document.
The user can combine and splice the information generated by the data into the < body > </body > node of the Word/document.xml file by calling the method, and then add the contact mapping and the head Word file identification with other files to generate the complete document.xml file.
The technical solutions in the embodiments of the present invention are fully described in a specific embodiment, and the described example is only a part of the examples of the present invention, and is not all embodiments.
1. And acquiring JSON data of the missed scan result, and forming related data according to the format, wherein the JSON data is driving data to be loaded into the Word document.
2. And extracting a public function, preparing a public style, managing the incidence relation of each file, and determining a word style template.
3. And the self-defining method is used for splicing and combining the contents to generate the corresponding XML. The method mainly comprises the methods of key Word generation, paragraph generation, picture generation, chart generation and sequence generation, and the methods can generate XML nodes according to condition judgment or circulation requirements, and can be called once and for many times and selected and combined freely. And finally, splicing the link connection and the head analysis file of other documents to generate an XML file of Word.
4. And acquiring a file set for generating the XML, compressing the file by using a JSZip compression technology, and storing the file stream by using the fileSeaver. FileSaver is a method for downloading and storing file stream from front end, and if the file stream is used in other technologies, the downloading mode of the corresponding file stream can be switched
According to the Word document generating method based on JSON data, a third-party plug-in is separated, the traditional mode that only Java language can generate Word is broken through, the method has wider adaptive performance, and the purpose of Word which can be deployed in any environment is achieved.
According to the Word document generating method based on JSON data, the method for extracting and generating keywords, generating paragraphs, generating tables, generating pictures and generating charts can freely define circulation and conditional statements, and Word documents can be conveniently modified and generated according to changes of business requirements.
According to the JSON data-based word document generation method, third-party plug-in conversion is replaced, and the process is simplified.
The method for generating Word documents is described in a specific embodiment as follows:
a. and setting driving data in a JSON format, and generating according to the outline level.
b. And extracting a public function, preparing a public style, managing the association relation of each file and determining a word style template.
c. The XML file is generated using various methods, as described below in the example of generating XML.
1) The generated keyword may be formatted according to requirements such as underlining or setting color and background.
2) Keywords or pictures can be inserted in the middle of the generated paragraph.
3) And generating a picture, inserting the data information of base64, and adding the picture into the document.
4) And generating a chart, storing the chart by an xlxs document, adding the data of the chart and the mapping relation of rows and columns, storing the chart as an xlsx table, and adding the corresponding relation of xlsx and chart.
5) A table is generated which may be arranged to create cells into which keywords are inserted, or paragraphs or pictures, etc.
d, acquiring a file set for generating XML, compressing the file by using JSZip compression technology, and storing file stream by using FileSaver
Those skilled in the art will appreciate that all or part of the steps implementing the above embodiments are implemented as computer programs executed by a CPU. When executed by the CPU, performs the functions defined by the methods provided herein. The program may be stored in a computer readable storage medium, which may be a read-only memory, a magnetic or optical disk, or the like.
Furthermore, it should be noted that the above-mentioned figures are only schematic illustrations of the processes involved in the method according to exemplary embodiments of the present application, and are not intended to be limiting. It will be readily understood that the processes shown in the above figures are not intended to indicate or limit the chronological order of the processes. In addition, it is also readily understood that these processes may be performed synchronously or asynchronously, e.g., in multiple modules.
The following are embodiments of the apparatus of the present application that may be used to perform embodiments of the method of the present application. For details which are not disclosed in the embodiments of the apparatus of the present application, reference is made to the embodiments of the method of the present application.
FIG. 5 is a block diagram illustrating a JSON data based word document generating apparatus in accordance with an exemplary embodiment. As shown in fig. 5, the word document generating apparatus 50 based on JSON data includes: a data module 502, a template module 504, an instruction module 506, a load module 508, and a compress module 510.
The data module 502 is used for generating a plurality of bottom layer data modules corresponding to chapters, tables and charts based on JSON data; the data module 502 is further configured to generate a chapter bottom layer data module, a secondary directory bottom layer data module, and a title bottom layer data module step by step according to the chapter title level; defining a table name and generating a bottom layer data module corresponding to the table name; and defining the icon name and generating a bottom layer data module corresponding to the icon name.
The template module 504 is used for extracting public functions and association relations to generate a word style template; the template module 504 is further configured to extract a file composition structure relationship according to a format of a word document to be generated; extracting a public function corresponding to the XML format file according to the file composition structure relation; and extracting the association relationship between the XML format file and other documents according to the file composition structure relationship.
The instruction module 506 is configured to obtain real-time online operations of a user and generate an operation instruction; the instruction module 506 is further configured to generate the operation instruction according to a real-time input command of the user, where the operation instruction is a JavaScript scripting language.
The loading module 508 is used for loading the bottom layer data module according to the operation instruction to generate an XML format file;
the compressing module 510 is configured to compress the XML format file to convert the XML format file into a word document displayed in real time. The compression module 510 is further configured to convert the picture into a base64 format when the operation instruction generates a picture instruction; and adding the incidence relation between the picture and the xml file in the file composition structure relation.
According to the word document generating device based on JSON data, a plurality of bottom layer data modules corresponding to chapters, tables and charts are generated based on the JSON data; extracting the public functions and the association relation to generate a word style template; acquiring real-time operation of a user on line and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; the XML format file is compressed to be converted into the word file displayed in real time, the problem that the prior art strongly depends on the operation environment can be solved, the problems that the development process is complicated and the error probability is high during program operation in the prior art are solved, and the problem that modification is inconvenient when the requirement changes in a template generation mode is solved.
FIG. 6 is a block diagram illustrating an electronic device in accordance with an example embodiment.
An electronic device 600 according to this embodiment of the present application is described below with reference to fig. 6. The electronic device 600 shown in fig. 6 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present application.
As shown in fig. 6, the electronic device 600 is in the form of a general purpose computing device. The components of the electronic device 600 may include, but are not limited to: at least one processing unit 610, at least one storage unit 620, a bus 630 that connects the various system components (including the storage unit 620 and the processing unit 610), a display unit 640, and the like.
Wherein the storage unit stores program code that can be executed by the processing unit 610, such that the processing unit 610 performs the steps according to various exemplary embodiments of the present application described in the present specification. For example, the processing unit 610 may perform the steps shown in fig. 2 and 3.
The storage unit 620 may include readable media in the form of volatile memory units, such as a random access memory unit (RAM) 6201 and/or a cache memory unit 6202, and may further include a read-only memory unit (ROM) 6203.
The memory unit 620 may also include programs/utilities 6204 having a set (at least one) of program modules 6205, such program modules 6205 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which, or some combination thereof, may comprise an implementation of a network environment.
Bus 630 may be one or more of several types of bus structures, including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
The electronic device 600 may also communicate with one or more external devices 600' (e.g., keyboard, pointing device, bluetooth device, etc.), such that a user can communicate with devices with which the electronic device 600 interacts, and/or any device (e.g., router, modem, etc.) with which the electronic device 600 can communicate with one or more other computing devices. Such communication may occur via an input/output (I/O) interface 650. Also, the electronic device 600 may communicate with one or more networks (e.g., a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network such as the Internet) via the network adapter 660. The network adapter 660 may communicate with the other modules of the electronic device 600 via the bus 630. It should be appreciated that although not shown in the figures, other hardware and/or software modules may be used in conjunction with the electronic device 600, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, among others.
Through the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein may be implemented by software, or by software in combination with necessary hardware. Therefore, as shown in fig. 7, the technical solution according to the embodiment of the present application may be embodied in the form of a software product, which may be stored in a non-volatile storage medium (which may be a CD-ROM, a usb disk, a removable hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which may be a personal computer, a server, or a network device, etc.) to execute the above method according to the embodiment of the present application.
Generally speaking, according to the method for generating the Word document by constructing the bottom XML and then compressing, the XML file is generated by calling the corresponding interface according to the driving data and loading and assembling through the single scripting language of JavaScript, and finally the Zip file of Docx is generated. First, JSON data is prepared. 1. For loading data, defining a set of JSON object data to perform corresponding assembly mapping, a specification format needs to be designed for the data to be loaded so that the data is easy to read and convenient to locate. The data specification is divided based on the outline and the title, and the modules are respectively divided step by step according to chapter title levels. The JSON format chapters are divided, JSON data of multiple modules are divided into multiple chapters, and the section I can be named as section1. And dividing the small modules according to a chapter secondary directory, wherein the chapter 1.1 is named as s1_1, the chapter 1.2 is named as s1 \\ 1 \/2, and so on. The chapter has no second-level directory division and is directly regarded as the chapter front part named as s1_0.2. To load the table, a table name is defined, chapter name + table identification '_ t', e.g., the first table designation of chapter 1.1, s1_, t1.3. To load a chart, a chart name is defined, chapter name + chart identification '_ e', e.g., the first chart designation of chapter 1.1, s1_, e1. And secondly, extracting a public function, preparing a public style, managing the incidence relation of each file and determining a Word style template. Generating Word is to compress series of files according to structure, wherein document is a core file for loading data into a document, and other files can be extracted from existing generated Word files or generated by using POI. Then, the custom method loads data and assembles XML. The method mainly comprises the steps of assembling and combining Word into document XML files, wherein the assembly is used for generating the XML files by a series of methods, and the following key methods are set for decomposing the content of the Word files: generating keywords, generating paragraphs, generating tables, generating charts and generating pictures. Generating a key word, wrapping the key word in the node of the < w: t > < w: r > </w: t >, generating the key word to generate the node of the < w: t > < w: r >, and configuring a related < w: rPr > </w: rPr > style. A paragraph is generated, and the paragraph is wrapped in the < w: p >. The < w: p > node and can contain a plurality of keywords, and the < w: pPr > </w: pPr > -can be configured. Generating a relatively complex point of the table, < w: tbl > </w: tbl > identifies the table, < w: tblPr > </w: tblPr > identifies the table style, lines are identified by < w: tc > </w: tc >, and < w: vMerge > can be subjected to cell merging. Generating a graph requires inserting a generated chart file and a corresponding excel file, and introducing a relationship therebetween. Generating an excel file by using the exportExcel, compressing the excel file into a Word/embeddings folder, inserting the corresponding charts file into the Word/charts folder, and finally adding the relationship between charts and xlsx. Only base64 data can be added in the generated picture, the picture is converted into the base64 data, and then the association between the picture and the document. The method is flexibly called, the data is generated by combining and splicing the information into the < body > </body > node of the Word/document.xml file, and then the contact mapping with other files and the head Word file identification are added to generate the complete document.xml file. And finally, compressing the multiple XML files into a Zip file, and synthesizing a DOCX document. The conversion of XML into Zip and then into Docx document is realized by using a compression method of JSZip files. JSZip is a tool for creating, reading and editing Zip by a JavaScript class library, wherein a generated file set is compressed into Zip. The generated Zip is the content of the file to be converted into Docx. Specifically, JSON data of the missed scan result, which is drive data to be loaded into a Word document, is acquired and formatted into relevant data. And extracting a public function, preparing a public style, managing the incidence relation of each file, and determining a word style template. And the self-defining method is used for splicing and combining the contents to generate the corresponding XML. The method mainly comprises the methods of key Word generation, paragraph generation, picture generation, chart generation and sequence generation, and the methods can generate XML nodes according to condition judgment or circulation requirements, and can be called once and for many times and selected and combined freely. And finally, splicing the link connection and the head analysis file of other documents to generate an XML file of Word. And acquiring a file set for generating the XML, compressing the file by using a JSZip compression technology, and storing the file stream by using the fileSeaver. FileSaver is a method for front-end downloading of a stream for saving a file, and if the file is used in other technologies, the downloading mode of the corresponding file stream can be switched. For example, driving data in JSON format is set and generated according to the outline level. And extracting a public function, preparing a public style, managing the association relation of each file and determining a word style template. The XML file is generated using various methods, as described below in the example of generating XML. The generated keyword may be formatted according to requirements such as underlining or setting color and background. Keywords or pictures can be inserted in the middle of the generated paragraph. And generating a picture, inserting the data information of base64, and adding the picture into the document. Generating a chart, needing an xlxs document to be stored, adding the data of the chart and the mapping relation of rows and columns, storing the data as an xlsx table, and adding the corresponding relation of xlsx and chart. A table is generated which may be arranged to create cells into which keywords are inserted, or paragraphs or pictures, etc. d. And acquiring a file set for generating the XML, compressing the file by using a JSZip compression technology, and storing the file stream by using FileSaver. The method and the system break away from a third-party plug-in, break through the mode that only Java language can generate Word in the past, have wider adaptive performance, and achieve the purpose of Word which can be deployed in any environment. The method for generating the document, extracting and generating the keywords, generating the paragraph, generating the table, generating the picture and generating the chart of the Word can freely define the cycle and the conditional statement, and can more conveniently modify and generate the Word file according to the change of the business requirement. And the conversion of a third-party plug-in is replaced, so that the process is simplified. The key points of the present disclosure are a series method for generating document. XML, including methods for generating keywords, paragraphs, pictures, tables and charts. And generating the whole flow of driving and generating the Word file by the data, starting from the data, extracting a public function, calling a self-defined generation method, and driving and generating the Word file.
The software product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium include: an electrical connection having one or more wires, a portable disk, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The computer readable storage medium may include a propagated data signal with readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A readable storage medium may also be any readable medium that is not a readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a readable storage medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Program code for carrying out operations of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C + + or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device and partly on a remote computing device, or entirely on the remote computing device or server. In situations involving remote computing devices, the remote computing devices may be connected to the user computing device through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to external computing devices (e.g., through the internet using an internet service provider).
The computer readable medium carries one or more programs which, when executed by a device, cause the computer readable medium to perform the functions of: generating a plurality of bottom layer data modules corresponding to chapters, tables and charts based on JSON data; extracting the public function and the incidence relation to generate a word style template; acquiring real-time operation of a user on line, and generating an operation instruction; loading a bottom layer data module according to the operation instruction to generate an XML format file; and compressing the XML format file to convert the XML format file into a word document displayed in real time.
Those skilled in the art will appreciate that the modules described above may be distributed in the apparatus according to the description of the embodiments, or may be modified accordingly in one or more apparatuses unique from the embodiments. The modules of the above embodiments may be combined into one module, or further split into multiple sub-modules.
Through the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein may be implemented by software, or by software in combination with necessary hardware. Therefore, the technical solution according to the embodiment of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a usb disk, a removable hard disk, etc.) or on a network, and includes several instructions to enable a computing device (which can be a personal computer, a server, a mobile terminal, or a network device, etc.) to execute the method according to the embodiment of the present application.
Exemplary embodiments of the present application are specifically illustrated and described above. It is to be understood that the application is not limited to the details of construction, arrangement, or method of implementation described herein; on the contrary, the intention is to cover various modifications and equivalent arrangements included within the spirit and scope of the appended claims.

Claims (11)

1. A word document generation method based on JSON data is characterized by comprising the following steps:
generating a plurality of bottom layer data modules corresponding to chapters, tables and charts based on JSON data;
extracting the public function and the incidence relation to generate a word style template;
acquiring real-time operation of a user and generating an operation instruction;
loading a bottom layer data module according to the operation instruction to generate an XML format file;
and compressing the XML format file to convert the XML format file into a word document displayed in real time.
2. The method of claim 1, wherein generating a plurality of underlying data modules corresponding to chapters, tables, charts based on JSON data comprises:
generating a chapter bottom layer data module, a secondary catalog bottom layer data module and a title bottom layer data module step by step according to the chapter title level;
defining a table name and generating a bottom layer data module corresponding to the table name;
and defining the icon name and generating a bottom layer data module corresponding to the icon name.
3. The method of claim 1, wherein extracting common functions and associations to generate a word style template comprises:
extracting files according to the format of the word document to be generated to form a structural relationship;
extracting a public function corresponding to the XML format file according to the file composition structure relationship;
and extracting the association relation between the XML format file and other documents according to the file composition structure relation.
4. The method of claim 1, wherein obtaining real-time operation of a user, generating an operation instruction, comprises:
and generating the operation instruction according to a real-time input command of a user, wherein the operation instruction is a JavaScript scripting language.
5. The method of claim 1, wherein loading the underlying data module to generate an XML-formatted file according to the operation instruction comprises:
when the operation instruction is a keyword instruction, extracting < w: t > < w: r > nodes from the XML format file;
configuring a < w: rPr > </w: rPr > style of a < w: t > < w: r > node according to the generated key instruction.
6. The method of claim 1, wherein loading the underlying data module to generate an XML-formatted file according to the operation instruction comprises:
when the operation instruction is a generation paragraph instruction, extracting nodes wrapping the (w: p > ... w: p >) in an XML format file;
and (3) packaging < w: p > \8230andthe < w: rPr > style of the node of the < w: p > according to the generation paragraph instruction configuration.
7. The method of claim 1, wherein loading an underlying data module to generate an XML-formatted file according to the operational instructions comprises:
when the operation instruction is a generate table instruction, identifying a table by < w: tbl > </w: tbl > based on the generate table instruction; and/or
Identifying a form style by < w: tblPr > </w: tblPr >, and/or based on the generate form instruction
Identifying rows by < w: tc > </w: tc > based on the generate table instruction; and/or
And carrying out cell generation through the vMerge based on the table generation instruction.
8. The method of claim 1, wherein loading an underlying data module to generate an XML-formatted file according to the operational instructions comprises:
when the operation instruction is a chart instruction, acquiring a relation between a chart file and an excel file corresponding to a chart;
compressing and placing excel files corresponding to the graph into a first preset position;
putting the chart corresponding chart into a second preset position;
the relationship between the charts file and the xlsx file is added in the file composition structure relationship.
9. The method of claim 1, wherein loading the underlying data module to generate an XML-formatted file according to the operation instruction comprises:
when the operation instruction generates a picture instruction, converting the picture into a base64 format;
and adding the incidence relation between the picture and the xml file in the file composition structure relation.
10. The method of claim 1, wherein compressing the XML-formatted file to convert to a word document for presentation in real-time comprises:
compressing the XML format file by a JSZip method; growing a zip file;
and converting the zip file into a word document displayed in real time.
11. A word document generating device based on JSON data is characterized by comprising:
the data module is used for generating a plurality of bottom layer data modules corresponding to chapters, tables and diagrams based on JSON data;
the template module is used for extracting the public functions and the incidence relation to generate a word style template;
the instruction module is used for acquiring real-time operation of a user on line and generating an operation instruction;
the loading module is used for loading the bottom layer data module according to the operation instruction to generate an XML format file;
and the compression module is used for compressing the XML format file to convert the XML format file into a word document displayed in real time.
CN202211712932.XA 2022-12-27 2022-12-27 Word document generation method and device based on JSON data Pending CN115983230A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202211712932.XA CN115983230A (en) 2022-12-27 2022-12-27 Word document generation method and device based on JSON data

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202211712932.XA CN115983230A (en) 2022-12-27 2022-12-27 Word document generation method and device based on JSON data

Publications (1)

Publication Number Publication Date
CN115983230A true CN115983230A (en) 2023-04-18

Family

ID=85966328

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202211712932.XA Pending CN115983230A (en) 2022-12-27 2022-12-27 Word document generation method and device based on JSON data

Country Status (1)

Country Link
CN (1) CN115983230A (en)

Similar Documents

Publication Publication Date Title
CN107832045B (en) Method and apparatus for cross programming language interface conversion
US9256583B2 (en) Conversion of a presentation to Darwin Information Typing Architecture (DITA)
TW201007563A (en) Mobile device application framework
CN111324833A (en) Page display method, device, electronic design and computer readable medium
CN113448562B (en) Automatic logic code generation method and device and electronic equipment
CN105589959A (en) Form processing method and form processing system
CN110795649A (en) Target page display method, device and system and electronic equipment
CN110688145B (en) Android MVP code automatic generation method and device, medium and electronic equipment
WO2023092580A1 (en) Page display method and apparatus, storage medium, and electronic device
WO2022184077A1 (en) Document editing method and apparatus, and terminal and non-transitory storage medium
CN110308907B (en) Data conversion method and device, storage medium and electronic equipment
CN111078228A (en) Method and device for converting webpage into small program, server and storage medium
CN114048415A (en) Form generation method and device, electronic equipment and computer readable storage medium
Taivalsaari Mashware: The future of web applications
CN110569488A (en) modular template WORD generation method based on XML (extensive markup language)
CN113761871A (en) Rich text rendering method and device, electronic equipment and storage medium
CN109408057B (en) Method, device, medium and computing equipment for automatically generating codes
CN108021317B (en) Method and device for screen editing
CN115983230A (en) Word document generation method and device based on JSON data
CN110399187A (en) A kind for the treatment of method and apparatus of language resource
CN113342450B (en) Page processing method, device, electronic equipment and computer readable medium
CN112394920B (en) Application software development method, platform and electronic equipment
CN113885841A (en) Script generation method and device, electronic equipment and readable medium
CN113778401A (en) Page generation method and device
CN112417827A (en) Method and device for editing and displaying online document, electronic equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination