首页 > 其他分享 >vue文档下载

vue文档下载

时间:2023-06-08 14:57:17浏览次数:38  
标签:vue const import tagValue 文档 error return data 下载

文档中{普通文字}{%图片}{%%居中图片}

<template>
    <el-date-picker
            v-model="value"
            class="timePicker"
            type="day"
            placeholder=""
            format="YYYY-MM-DD "
            value-format="YYYY-MM-DD">
    </el-date-picker>
    <el-button type="primary" size="mini" @click="Export()">导出word</el-button>
</template>

<script>
    import exportWord from '@/utils/export-word'
    export default {
        name: "Chartgeneration",
        mixins:[exportWord],
        data() {
        },
        data: function() {
            return {
                value: ''
            }
        },
    methods:{
        Export:function () {
            const data = {
                monitoringLine:'长白线',
                measuringpointKilometerPost:'K268+260',
                measuringpointName:'43#基站',
                cableModel:'SPTYWPL23',
                cableLength:'3.5km',
                start: '2022',
                JSON: 'json',
                ex: 'docx.js, docx4j, python-docx'
            }
            this.exportWord(data)
        }
        }
}
</script>

export-word.js

import docxtemplater from 'docxtemplater'
import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
import { saveAs } from 'file-saver'
import * as echarts from 'echarts';
import ImageModule from 'docxtemplater-image-module-free'
export default {
    methods: {
        // 导出echarts图片,格式转换
        base64DataURLToArrayBuffer(dataURL) {
            const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
            if (!base64Regex.test(dataURL)) {
                return false;
            }
            const stringBase64 = dataURL.replace(base64Regex, "");
            let binaryString;
            if (typeof window !== "undefined") {
                binaryString = window.atob(stringBase64);
            } else {
                binaryString = new Buffer(stringBase64, "base64").toString("binary");
            }
            const len = binaryString.length;
            const bytes = new Uint8Array(len);
            for (let i = 0; i < len; i++) {
                const ascii = binaryString.charCodeAt(i);
                bytes[i] = ascii;
            }
            return bytes.buffer;
        },
   
        //echarts
        exportWord(
            data,
            docTitle = '题目'
        ) {
            const div = document.createElement('div');
            div.setAttribute('style', 'width: 560px;height:280px;')

            const myChart = echarts.init(div)
            myChart.setOption({
                title: {
                    text: '示例'
                },
                tooltip: {},
                legend: {
                    data: ['销量']
                },
                xAxis: {
                    data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
                },
                yAxis: {},
                series: [{
                    name: '销量',
                    type: 'bar',
                    data: [5, 20, 36, 10, 10, 20]
                }]
            })
            let _this=this
            setTimeout(() => { // 加setTimeout是为了让echarts渲染完成后生成图片
                JSZipUtils.getBinaryContent('/auditResult.docx', function (error, content) {
                    // 抛出异常
                    if (error) {
                        throw error
                    }
                    const opts = {
                        centered: false,
                        fileType: 'docx'
                    }
                    opts.getImage = function (tagValue) {
                        if (tagValue.size && tagValue.data) {
                            return _this.base64DataURLToArrayBuffer(tagValue.data);
                        }
                        return _this.base64DataURLToArrayBuffer(tagValue);
                    };
                    opts.getSize = function (_, tagValue) {
                        if (tagValue.size && tagValue.data) {
                            return tagValue.size;
                        }
                        return [560, 280];
                    };
                    // 创建一个JSZip实例,内容为模板的内容
                    const zip = new PizZip(content)
                    // 创建并加载docxtemplater实例对象
                    const doc = new docxtemplater().loadZip(zip)

                    doc.setOptions({
                        nullGetter: function () { //设置空值 undefined 为''
                            return '';
                        },
                    });
                    // 设置图片模块
                    doc.attachModule(new ImageModule(opts));

                    // 设置模板变量的值
                    doc.setData({
                        ...data,
                        image:myChart.getDataURL({
                            pixelRatio: 5, //导出的图片分辨率比率,默认是1
                            backgroundColor: '#fff', //图表背景色
                            excludeComponents: ['toolbox'], //忽略组件的列表
                            type: 'png', //图片类型支持png和jpeg
                        }),
                    })
                    try {
                        // 用模板变量的值替换所有模板变量
                        doc.render()
                    } catch (error) {
                        // 抛出异常
                        // const e = {
                        //   message: error.message,
                        //   name: error.name,
                        //   stack: error.stack,
                        //   properties: error.properties,
                        // }
                        console.error('导出报表失败')
                        throw error
                    }
                    // 生成一个代表docxtemplater对象的zip文件(不是一个真实的文件,而是在内存中的表示)
                    const out = doc.getZip().generate({
                        type: 'blob',
                        mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                    })
                    // 将目标文件对象保存为目标类型的文件,并命名
                    saveAs(out,`${docTitle}.doc`)
                })
            }, 1000);
        }
    }
}

标签:vue,const,import,tagValue,文档,error,return,data,下载
From: https://www.cnblogs.com/watm/p/17466476.html

相关文章

  • Vue3从入门到精通(二)
    vue3侦听器在Vue3中,侦听器的使用方式与Vue2相同,可以使用watch选项或$watch方法来创建侦听器。不同之处在于,Vue3中取消了immediate选项,同时提供了新的选项和API。创建侦听器可以使用watch选项或$watch方法来创建侦听器,语法与Vue2相同。示例如下:<template><div>{{messa......
  • 【Linux】上传下载文件命令rz、sz
    安装要使用rz、sz命令传输文件需要给服务器安装lrzsz:yum-yinstalllrzsz上传上传文件只需在shell终端仿真器中输入命令“rz”,即可从弹出的对话框中选择本地磁盘上的文件,上传到服务器当前路径下。【选择需要上传文件,一次可以指定多个文件】下载下载文件只需在shell终端......
  • 解决Vue项目在刷新页面时出现404错误的问题
    使用HTML5的history模式的问题在本地运行Vue项目时,可以直接点击路由跳转,并且刷新页面也没有问题。这是因为VueRouter默认使用HTML5的history模式,它通过修改浏览器历史记录来控制页面跳转,而不发送实际的HTTP请求。然而,当将Vue项目发布到服务器上时,服务器会根据实际的HTTP请求来......
  • Swagger 自定义输出文档
    Swagger作为Java开发中常用的接口文档生成组件,绝大部分情况下都可以满足我们的业务需求。但有些时候依然有些不足,比如针对系统中某些特殊接口需要作出一些增强。我自己在使用Swagger时候遇到需要将base_path消除并直接拼接到接口路径上的情形。一共寻找到两种方式......
  • python 解析HTML和XML文档
    一、BeautifulSoupBeautifulSoup是一个Python包,用于解析HTML和XML文档。它可以快速而方便地从网页中提取信息,并以易于使用的方式对其进行处理。它支持各种解析器,包括内置的Python解析器和第三方解析器,例如lxml和html5lib。二、对标签提取代码示列以下是使用BeautifulSoup解析H......
  • Vue进阶(幺幺捌):CSS3 - 选择器first-child、last-child、nth-child、nth-last-child、nt
    (文章目录)1.first-child(IE7兼容)、last-child(IE8不兼容)html:<body><h2>列表</h2><ul><li>列表项目1</li><li>列表项目2</li><li>列表项目3</li><li>列表项目4</li></ul></body&g......
  • VUE Error: Cannot call .tap() on a plugin that has not yet been defined. Call pl
    在对一个vue项目执行过“npmauditfix--force”命令之后,就出现了如下错误: ERROR Error:Cannotcall.tap()onapluginthathasnotyetbeendefined.Callplugin('preload').use(<Plugin>)first.有2个解决方法:方法一:删除之前的源码模块,重新下载后执行“npminstall......
  • Vue-cli
    目录Vue-CLI1单文件组件2vue-cli创建项目3.1vue-cli创建项目3.1.1vue-cli命令行创建项目3.1.2使用vue-cli-ui创建4vue项目目录结构4.1运行vue项目4.2vue项目的目录结构5vue项目编写规范5.1修改项目5.2以后写vue项目,只需要在固定位置写固定代码即可6es6导入导出语法默......
  • python读txt文档-多列
    有一个txt格式的文本文档,格式如下。有两行数据。3个字段,字段与字段直接使用tab键分割开。hello1world1hellothankyou1hello2world2hellothankyou2现在想通过python读取这个文件。分别读取到hello1,world1,和 hellothankyou1代码如下。withopen('......
  • Vue零基础开发入门
    讲解部分Vue基础语法,通过TodoList功能编写,在熟悉基础语法的基础上,扩展解析MVVM模式及前端组件化的概念及优势。1案例1.1下载安装https://v2.cn.vuejs.org/v2/guide/installation.html:1.2原生实现打印<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8......