Vue实现excel导出,不请求后端

原因是做出来的不符合需求,需要连接后端,又不想删掉,做个记录

1:先创建一个按钮

html 复制代码
 <el-col :span="1.5">
            <el-button
              @click="exportExcel"
              size="mini"
              type="primary"
              icon="el-icon-download"
            >导出Excel</el-button>
</el-col>

2:先查看是XLSX的依赖,没有的话需要安装一下

html 复制代码
npm install xlsx

安装的时候如果报错需要清理缓存,并把依赖包删掉重新安装

node_modules为项目中的依赖包

html 复制代码
npm cache clean --force

//linux
rm -rf node_modules
//windows
Remove-Item -Recurse -Force node_modules

npm install

出现这个问题的话需要,重新编译node-sass

Found bindings for the following environments: - Windows 64-bit with Node.js 10.x This usually happens because your environment has changed since running npm install. Run npm rebuild node-sass to download the binding for your current environment.

html 复制代码
npm rebuild node-sass

3:然后引入

html 复制代码
<script>
import XLSX from 'xlsx';

4:添加导出方法

html 复制代码
method:{
exportExcel() {
      // 导出Excel的方法
      if(this.list.length > 0){
      	//此处的this.list为列表的数据集合
        const data = this.list.map(item => {
          return {
            '列名1': item.数据名称,
            '列名2': item.数据名称,
            '列名3': item.数据名称,
            '列名4': item.数据名称
          };
        });
        const worksheet = XLSX.utils.json_to_sheet(data);
        const columnWidths = [
          { wch: 20 },  // 第一列宽度为20
          { wch: 15 },  // 第二列宽度为15
          { wch: 10 },  // 第三列宽度为10
          { wch: 15 }   // 第四列宽度为15
        ];
        worksheet['!cols'] = columnWidths;
        const workbook = {
          Sheets: { 'data': worksheet },
          SheetNames: ['data']
        };
        const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });
        const blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8' });
        const filename = '文件名.xlsx';
        if (typeof navigator.msSaveBlob === 'function') {
          navigator.msSaveBlob(blob, filename);
        } else {
          const link = document.createElement('a');
          if (link.download !== undefined) {
            const url = URL.createObjectURL(blob);
            link.setAttribute('href', url);
            link.setAttribute('download', filename);
            link.style.visibility = 'hidden';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          }
        }
      }else{
        this.$message.error('暂无数据');
      }
    },
   }
相关推荐
gis开发之家17 分钟前
《Vue3 从入门到大神33篇》Vue3 源码详解(三):响应式核心思想——从 Object.defineProperty 到 Proxy
前端·javascript·vue.js·vue3·vue3源码
Hyyy26 分钟前
流式渲染的基础——SSE
前端·后端·面试
Csvn40 分钟前
📐 Layout Thrashing——一个 `offsetHeight` 让你的页面卡成 PPT
前端
qq_529599381 小时前
react js循环滚动
前端·javascript·react.js
濮水大叔1 小时前
在 DTO 中使用 Form Layout 配置复杂表单,支持 Tabs/Groups/Sections
vue.js·typescript·node.js
索西引擎1 小时前
【React】严格模式:开发阶段质量保障的静态分析机制
前端·react.js·前端框架
Java面试题总结1 小时前
使用 Python 在 Excel 中添加和自定义文本框
开发语言·python·excel
张人玉2 小时前
Vue 3 + ECharts 智慧城市大屏——赣州智慧城市数据可视化平台
vue.js·echarts·智慧城市
玉宇夕落2 小时前
详细流式输出学习
前端
秃头披风侠_郑2 小时前
【uniapp】一文让你学会微信小程序+APP+H5全平台实战指南
前端·微信小程序·uni-app