Get the App
SLTechnology News&Howtos  ›  Development  › 

How does vue export word

Shulou Source: shulou.com Published: 2022-05-31 21:02:42 10月03日 Update

This article mainly introduces "how to derive word from vue". In daily operation, I believe many people have doubts about how to derive word from vue. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to derive word from vue". Next, please follow the editor to study!

Vue export word pure front-end implementation

First, create a new docx file and write the template first.

Attention! If an array exists in the data structure. Package with {# xxx} {/ xxx}.

Example of data structure:

WordData: {name: 'export word', nameList: [{name: "Zhang San", age: 16, hobby: [' eat', 'sleep', 'hit Doudou']}, {name: "Li Si", age: 19 Hobby: ['smoking', 'drinking', 'playing mahjong'},]}

After the template is written, put it into the project.

Then import the required package

Npm i docxtemplater pizzip file-saver-save

Then introduce it into the required module

Import 'docxtemplater/build/docxtemplater.js' import' pizzip/dist/pizzip.js' import 'pizzip/dist/pizzip-utils.js' import' file-saver' methods: {/ / Export word loadFile (url, callback) {PizZipUtils.getBinaryContent (url, callback);}, generate () {var that = this This.loadFile (".. / static/word.docx", function (error, content) {if (error) {throw error}; var zip = new PizZip (content); var doc = new window.docxtemplater () .loadZip (zip) doc.setData ({... that.wordData}) Try {/ / render the document (replace all occurences of {first_name} by John, {last_name} by Doe,...) Doc.render ()} catch (error) {var e = {message: error.message, name: error.name, stack: error.stack, properties: error.properties,} console.log (JSON.stringify ({error: e})) / / The error thrown here contains additional information when logged with JSON.stringify (it contains a property object). Throw error;} var out = doc.getZip (). Generate ({type: "blob", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",}) / / Output the document using Data-URI saveAs (out, "output.docx")}),}

That's it.

Complete code, just run it directly after installing the package. Remember to put in the word template!

Export import 'docxtemplater/build/docxtemplater.js' import' pizzip/dist/pizzip.js' import 'pizzip/dist/pizzip-utils.js' import' file-saver' export default {data () {return {wordData: {name: 'Export word', nameList: [{name: "Zhang San", age: 16 Hobby: ['eat', 'sleep', 'hit Doudou']}, {name: "Li Si", age: 19, hobby: ['smoking', 'drinking', 'playing mahjong'},]},}} Methods: {/ / Export word loadFile (url, callback) {PizZipUtils.getBinaryContent (url, callback) }, generate () {var that = this; this.loadFile (".. /.. / static/word.docx", function (error, content) {if (error) {throw error}; var zip = new PizZip (content) Var doc = new window.docxtemplater () .loadZip (zip) doc.setData ({... that.wordData}); try {/ / render the document (replace all occurences of {first_name} by John, {last_name} by Doe,...) Doc.render ()} catch (error) {var e = {message: error.message, name: error.name, stack: error.stack, properties: error.properties,} console.log (JSON.stringify ({error: e})) / / The error thrown here contains additional information when logged with JSON.stringify (it contains a property object). Throw error } var out = doc.getZip (). Generate ({type: "blob", mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",}) / / Output the document using Data-URI saveAs (out, "output.docx")},}}, this ends the study of "how vue derives word". I hope I can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Learning templates data data structures more structures mahjong Zhang San Li Si Doudou help practical next code front end packages arrays files articles methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB macOS Xiaomi Apple Microsoft