欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue使用file-saver本地文件導(dǎo)出功能

 更新時(shí)間:2022年01月07日 15:36:37   作者:前端辣白菜  
這篇文章主要介紹了vue使用file-saver本地文件導(dǎo)出,大家需要安裝xlsx和file-saver,然后創(chuàng)建localExports.js文件,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧

1:安裝xlsx和file-saver

npm install file-saver xlsx  --save

2:創(chuàng)建localExports.js文件

3:直接上代碼

import XLSX from 'xlsx';
const FileSaver = require('file-saver');
import { getRandomNum } from '@/utils';
// 本地導(dǎo)出表格
/**
 * 導(dǎo)出Excel文件
 * @param {*} elementName   table組件id名稱
 * @param {*} fileName      文件名
 * @description             使用說明
 *                          import { exportsXlsx } from '@/utils/localExports';
 *                          exportsXlsx('idName', '文件名稱');
 */
 
export function exportsXlsx(elementName, fileName) {
  const time = new Date().getTime();
  const random = getRandomNum(100, 1000);
  const wb = XLSX.utils.table_to_book(clearHead(elementName), { raw: true });
  const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' });
  FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `${fileName}${time}-${random}.xlsx`);
}
function clearHead(elementName) {
  const tableDom = document.querySelector('#' + elementName).cloneNode(true);
  const tableHeader = tableDom.querySelector('.el-table__header-wrapper');
  const tableBody = tableDom.querySelector('.el-table__body');
  tableHeader.childNodes[0].append(tableBody.childNodes[1]);
  const headerDom = tableHeader.childNodes[0].querySelectorAll('th');
 
  // 移除左側(cè)checkbox的節(jié)點(diǎn)
  if (headerDom[0].querySelectorAll('.el-checkbox')) {
    headerDom[0].remove();
  }
  for (const key in headerDom) {
    if (headerDom[key].innerText === '操作') {
      headerDom[key].remove();
    }
  }
  // 清理掉checkbox 和操作的button
  const tableList = tableHeader.childNodes[0].childNodes[2].querySelectorAll('td');
  for (let key = 0; key < tableList.length; key++) {
    if (tableList[key].querySelectorAll('.el-checkbox').length > 0 || tableList[key].querySelectorAll('.el-button').length > 0) {
      tableList[key].remove();
    }
  }
  return tableHeader;
}
 

4:使用方式

<el-table
          id="good"
          v-loading="listLoading"
          :header-cell-style="{ background: '#FAFAFA', color: '#212532' }"
          :data="list"
          tooltip-effect="dark"
          style="width: 100%"
          height="566"
          border
          @selection-change="handleSelectionChange"
        >
 
import { exportsXlsx } from '@/utils/localExports';
methods:{
  onSearch() {
      exportsXlsx('good', '模擬數(shù)據(jù)');
    
    },
}

5:good為table組件的id,getRamdomNum方法如下


// 生成隨機(jī)數(shù)
export function getRandomNum(Min, Max) {
  var Range = Max - Min;
  var Rand = Math.random();
  return (Min + Math.round(Rand * Range));
}

到此這篇關(guān)于vue使用file-saver本地文件導(dǎo)出的文章就介紹到這了,更多相關(guān)vue file-saver本地文件導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論