vxe-table 表格导出 pdf 文件

通过手动调用 exportData 方法可以直接导出文件

npm install vxe-pc-ui@4.3.2 vxe-table@4.9.3 @vxe-ui/plugin-export-pdf@4.0.8 jspdf@2.5.2
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/jspdf@2.5.2/dist/jspdf.umd.js"></script>
// ...
import { VxeUI } from 'vxe-pc-ui'
import VxeUIPluginExportPDF from '@vxe-ui/plugin-export-pdf'
// ...

// 确保 window.jsPDF 变量存在即表示安装完成
VxeUI.use(VxeUIPluginExportPDF, {
  // 安装中文字体
  // fontName: 'SourceHanSans-Normal',
  // fonts: [
  //   {
  //     fontName: 'SourceHanSans-Normal',
  //     fontUrl: 'https://cdn.jsdelivr.net/npm/@vxe-ui/plugin-export-pdf@4.0.8/fonts/source-han-sans-normal.js' // 字体库非常大,建议下载到项目本地
  //   }
  // ]
})

<template>
  <div>
    <vxe-button @click="exportEvent">高级导出</vxe-button>
    <vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script>
export default {
  data () {
    const gridOptions = {
      border: true,
      showFooter: true,
      exportConfig: {
        type: 'pdf'
      },
      columns: [
        { field: 'seq', type: 'seq', width: 70 },
        {
          title: '分组1',
          children: [
            { field: 'name', title: 'Name' },
            { field: 'role', title: 'Role' }
          ]
        },
        { field: 'sex', title: 'Sex' },
        { field: 'age', title: 'Age' }
      ],
      data: [
        { id: 10001, name: '张三', role: 'Develop', sex: 'Man', age: 28, address: 'test abc' },
        { id: 10002, name: '李四', role: 'Test', sex: 'Women', age: 22, address: '广东省' },
        { id: 10003, name: '王五', role: 'PM', sex: 'Man', age: 32, address: '上海' },
        { id: 10004, name: '老六', role: 'Designer', sex: 'Women', age: 24, address: 'Shanghai' }
      ],
      footerData: [
        { seq: '合计', name: '888人', age: 666 }
      ]
    }
    return {
      gridOptions
    }
  },
  methods: {
    exportEvent () {
      const $grid = this.$refs.gridRef
      if ($grid) {
        $grid.openExport()
      }
    }
  }
}
</script>

https://github.com/x-extends/vxe-table

posted @ 2024-11-21 15:15  可不简单  阅读(0)  评论(0编辑  收藏  举报