使用vue導出excel遇到的那些坑(開頭數字為0會被忽略掉,訂單號或者銀行卡號過長會被轉換成科學計數)


需求:

Vue+element UI el-table下的導出當前所有數據到一個excel文件里。

先按照網上的方法,看看有哪些坑

准備工作:

1、安裝依賴:yarn add xlsx file-saver -S

2、在放置需要導出功能的組件中引入
  

import FileSaver from "file-saver";
import XLSX from "xlsx";

 

3、HTML中的設置,簡單來說就是給需要導出的table標簽el-table上加一個id:如outTable,對應下面的exportExcel方法中的 document.querySelector(‘#outTable‘)

   //導出當前表格
exportCurrent:function(){
    var wb = XLSX.utils.table_to_book(document.querySelector('#outTable')) //表格id
    var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
    try {
        FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'sheet.xlsx')  //文件名
    } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
    return wbout
},

我們來看一下原始數據


接下來再來看一下導出的結果


哎???我訂單編號跟銀行卡號咋成了科學計數法了??

還有我的時間,時分秒呢??

原因是因為數字太長了,需要使用excel的文本格式才能顯示正常

經過各種搜索,最終解決方法如下:

exportExcel() {
      var xlsxParam = { raw: true };//轉換成excel時,使用原始的格式
      var wb = XLSX.utils.table_to_book(document.querySelector("#outTable"),xlsxParam);
      var wbout = XLSX.write(wb, {
        bookType: "xlsx",
        bookSST: true,
        type: "array"
      });
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: "application/octet-stream;charset=utf-8" }),
          "sheetjs.xlsx"
        );
      } catch (e) {
        if (typeof console !== "undefined") console.log(e, wbout);
      }
      return wbout;
    },

再來看我們的數據


大功告成

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM