Vue3+Quasar 前端导出

Ps:用的是quasar框架 Table 自带的导出 exportFile 功能, 并做了一些封装调整;

新建download.ts文件

//download.ts
import {exportFile, useQuasar} from "quasar";
const $q = useQuasar();

export function wrapCsvValue (val:any, formatFn:any) {
  let formatted = formatFn !== void 0 ? formatFn(val) : val;
  formatted = formatted === void 0 || formatted === null ? '' : String(formatted);
  formatted = formatted.split('"').join('""');
  return `"${formatted}"`;
}

export function exportDataCsv(columns: any[], Data: any[],fileName:string){
  // @ts-ignore
  // @ts-ignore

  const content = [columns.map(col => wrapCsvValue(col.label))].concat(
    Data.map(row => columns.map(col => wrapCsvValue(
      typeof col.field === 'function'
        ? col.field(row)
        : row[ col.field === void 0 ? col.name : col.field ],
      col.format,
    )).join(','))
  ).join('\r\n')
  const status = exportFile(
    `${fileName}.csv`,
    content, {
      mimeType: 'text/csv',
      byteOrderMark: '\uFEFF', //解决乱码问题
      encoding:'utf-8'
    },
  )
  if (status !== true) {
    $q.notify({
      message: 'Browser denied file download...',
      color: 'negative',
      icon: 'warning'
    })
  }
}

Vue代码

import {exportDataCsv} from "src/untils/file/download";
<q-btn outline no-wrap :loading="loading" label="导出" icon="file_download" color="primary"   @click="exportExcel('字典值列表')"/>
const exportExcel = (fileName) =>{
  exportDataCsv(state.columns,state.typeList,fileName);
}

你可能感兴趣的:(前端,javascript,vue.js)