Vue 把表格导出 Excel 文件及打印

安装文件

  1. 安装 xlsx

    npm install xlsx
    
  2. 安装 file-saver

    npm install file-saver
    
  3. 如果需要打印表格,安装 vue-print-nb

    npm install vue-print-nb
    

导出表格

主要使用的是 js-xlsx,导出 Excel 文件,主要是如何生成一个 sheet,之后将这个 sheet 转成最终 Excel 文件的 blob 对象,然后利用 URL.createObject 下载

  1. 配置生成 Excel 的配置项

    bookType 要生成的文件类型

    bookSST 是否生成 Shared String Table,如果开启生成速度会下降,但在低版本 IOS 设备上有更好的兼容性

    type 可以使用 base64binary (BinaryString)、string (utf8 字符串)、buffer(node buffer) 、array(Unit8Array 8位无符号数组) 、file (文件路径,仅 node 支持)

  2. 字符串转 ArrayBuffer 进行文件下载,这里使用 file-saver

    FileSaver.saveAs(Blob/File/Url, filename)

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

function htmlToExcel(dom, title = '默认标题') {
    
    
  const wb = XLSX.utils.table_to_book(document.querySelector(dom))
  const wbout = XLSX.write(wb, {
    
     bookType: 'xlsx', bookSST: true, type: 'array' })
  const blob = new Blob([wbout], {
    
     type: 'application/octet-stream' })
  FileSaver.saveAs(blob, `${
      
      title}.xlsx`)
}
  • 组件里使用。直接调用 htmlToExcel 方法,传入选择器和标题名称即可
<el-table id="printTest" :data="tableData" style="width: 500px">
  <el-table-column prop="date" label="日期"></el-table-column>
  <el-table-column prop="name" label="姓名"></el-table-column>
  <el-table-column prop="age" label="年龄"></el-table-column>
</el-table>
<el-button type="success" @click="htmlToExcel('#printTest', '表格')">导出Excel</el-button>

打印表格

main.js

import Print from 'vue-print-nb'

Vue.use(Print)

组件里使。用 v-print 指定需要打印的容器,如下示例 "'#printTest'"

  • 注意:选择器需要用单引号包裹
<template>
  <div>
    <el-table id="printTest" :data="tableData" style="width: 500px">
      <el-table-column prop="date" label="日期"></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>
    <el-button type="primary" v-print="'#printTest'">打印</el-button>
  </div>
</template>

Guess you like

Origin blog.csdn.net/qq_38689395/article/details/117563920