当前位置:首页 > VUE

vue表格实现导出

2026-02-17 03:32:01VUE

使用 vue-json-excel 插件

安装插件:

npm install vue-json-excel --save

在组件中引入并注册:

import JsonExcel from 'vue-json-excel'

export default {
  components: {
    JsonExcel
  },
  data() {
    return {
      json_data: [
        {name: '张三', age: 25},
        {name: '李四', age: 30}
      ],
      json_fields: {
        '姓名': 'name',
        '年龄': 'age'
      },
      filename: '用户数据'
    }
  }
}

模板中使用:

<download-excel
  :data="json_data"
  :fields="json_fields"
  :name="filename">
  <button>导出Excel</button>
</download-excel>

使用 xlsx 库

安装依赖:

npm install xlsx file-saver --save

创建导出方法:

import 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'
      )
    }
  }
}

自定义表格导出

处理表格数据:

export default {
  methods: {
    exportTable() {
      const table = this.$refs.table
      const wb = XLSX.utils.table_to_book(table)
      const wbout = XLSX.write(wb, {bookType: 'xlsx', type: 'array'})
      FileSaver.saveAs(
        new Blob([wbout], {type: 'application/octet-stream'}),
        '表格数据.xlsx'
      )
    }
  }
}

处理复杂数据格式

格式化日期数据:

methods: {
  formatData(data) {
    return data.map(item => ({
      ...item,
      createTime: this.$moment(item.createTime).format('YYYY-MM-DD')
    }))
  }
}

合并多级表头:

const ws = XLSX.utils.json_to_sheet(this.formatData(tableData))
ws['!merges'] = [
  {s: {r:0,c:0}, e: {r:0,c:3}}
]

导出 CSV 格式

使用纯前端方案:

exportCSV() {
  const csvContent = this.tableData
    .map(row => Object.values(row).join(','))
    .join('\n')
  const blob = new Blob([csvContent], {type: 'text/csv;charset=utf-8;'})
  FileSaver.saveAs(blob, 'data.csv')
}

服务端导出方案

前端请求:

exportServerExcel() {
  axios.post('/api/export', {
    params: this.searchParams
  }, {
    responseType: 'blob'
  }).then(res => {
    const blob = new Blob([res.data], {type: 'application/vnd.ms-excel'})
    FileSaver.saveAs(blob, 'server_data.xlsx')
  })
}

服务端示例(Node.js):

const excel = require('exceljs')
router.post('/export', async (req, res) => {
  const workbook = new excel.Workbook()
  const worksheet = workbook.addWorksheet('Sheet1')
  // 添加表头和数据
  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表格实现导出

标签: 表格vue
分享给朋友:

相关文章

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <l…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue实现keepalive

vue实现keepalive

Vue 中实现 keep-alive 在 Vue 中,keep-alive 是一个内置组件,用于缓存动态组件或组件的状态,避免重复渲染和销毁。以下是实现 keep-alive 的几种方法: 基本用法…

vue实现监控

vue实现监控

Vue 实现监控功能的方法 在 Vue 中实现监控功能通常涉及数据变化监听、事件监听或第三方监控工具集成。以下是几种常见实现方式: 使用 Vue 的 watch 监听数据变化 通过 watch 可以…