Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does vue export word

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 302

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report