当前位置:首页 > VUE

vue实现导出excel模板

2026-01-20 14:51:06VUE

使用 vue-json-excel 插件

安装 vue-json-excel 插件

npm install vue-json-excel --save

在 main.js 中引入并注册组件

import Vue from 'vue'
import JsonExcel from 'vue-json-excel'

Vue.component('downloadExcel', JsonExcel)

在组件中使用

<download-excel
  :data="json_data"
  :fields="json_fields"
  name="filename.xls">
  <button>导出 Excel</button>
</download-excel>
export default {
  data() {
    return {
      json_fields: {
        '姓名': 'name',
        '年龄': 'age',
        '地址': 'address'
      },
      json_data: [
        {
          name: '张三',
          age: 25,
          address: '北京'
        },
        {
          name: '李四',
          age: 30,
          address: '上海'
        }
      ]
    }
  }
}

使用 xlsx 和 file-saver 库

安装所需依赖

npm install xlsx file-saver --save

创建导出方法

import * as XLSX from 'xlsx'
import FileSaver from 'file-saver'

export default {
  methods: {
    exportExcel() {
      const data = [
        ['姓名', '年龄', '地址'],
        ['张三', 25, '北京'],
        ['李四', 30, '上海']
      ]

      const ws = XLSX.utils.aoa_to_sheet(data)
      const wb = XLSX.utils.book_new()
      XLSX.utils.book_append_sheet(wb, ws, 'Sheet1')

      const wbout = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })
      FileSaver.saveAs(
        new Blob([wbout], { type: 'application/octet-stream' }),
        '导出数据.xlsx'
      )
    }
  }
}

使用模板文件导出

准备 Excel 模板文件并上传到项目静态资源目录

实现模板填充功能

import * as XLSX from 'xlsx'

export default {
  methods: {
    async fillTemplate() {
      const response = await fetch('/static/template.xlsx')
      const arrayBuffer = await response.arrayBuffer()

      const workbook = XLSX.read(arrayBuffer)
      const worksheet = workbook.Sheets[workbook.SheetNames[0]]

      // 填充数据到模板
      worksheet['B2'].v = '张三'
      worksheet['C2'].v = 25
      worksheet['D2'].v = '北京'

      const wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' })
      FileSaver.saveAs(
        new Blob([wbout], { type: 'application/octet-stream' }),
        '填充后的模板.xlsx'
      )
    }
  }
}

后端配合导出方案

前端请求导出接口

export default {
  methods: {
    exportFromBackend() {
      axios.get('/api/export/excel', {
        responseType: 'blob'
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]))
        const link = document.createElement('a')
        link.href = url
        link.setAttribute('download', '后端导出数据.xlsx')
        document.body.appendChild(link)
        link.click()
      })
    }
  }
}

后端示例(Node.js)

const excel = require('exceljs')

router.get('/api/export/excel', async (req, res) => {
  const workbook = new excel.Workbook()
  const worksheet = workbook.addWorksheet('Sheet1')

  worksheet.columns = [
    { header: '姓名', key: 'name' },
    { header: '年龄', key: 'age' },
    { header: '地址', key: 'address' }
  ]

  worksheet.addRow({ name: '张三', age: 25, address: '北京' })
  worksheet.addRow({ name: '李四', age: 30, address: '上海' })

  res.setHeader(
    'Content-Type',
    'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
  )
  res.setHeader(
    'Content-Disposition',
    'attachment; filename=export.xlsx'
  )

  await workbook.xlsx.write(res)
  res.end()
})

vue实现导出excel模板

标签: 模板vue
分享给朋友:

相关文章

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router…

vue穿梭框组件实现

vue穿梭框组件实现

实现基础穿梭框组件 在Vue中实现穿梭框组件,可以使用<el-transfer>(Element UI)或手动实现。以下是手动实现的核心逻辑: <template> &…

vue实现字母添加排序

vue实现字母添加排序

实现字母添加排序的方法 在Vue中实现字母添加排序功能,可以通过以下步骤完成: 创建Vue实例并定义数据 new Vue({ el: '#app', data: { letters…

vue实现上移下移插件

vue实现上移下移插件

实现上移下移功能的 Vue 插件 在 Vue 中实现列表项的上移和下移功能,可以通过自定义指令或组件封装的方式完成。以下是两种常见实现方式: 基于数组操作的通用方法 通过操作数组索引实现元素位置交换…

vue实现打印二维码

vue实现打印二维码

安装二维码生成库 使用 qrcode 或 vue-qrcode 库生成二维码。通过 npm 安装: npm install qrcode vue-qrcode 生成二维码 在 Vue 组件中引…

vue实现mvc

vue实现mvc

Vue 实现 MVC 模式 Vue 本身是一个 MVVM(Model-View-ViewModel)框架,但可以通过结构调整实现 MVC(Model-View-Controller)模式。以下是具体实…