current position:Home>Front end export excel, JS xlsx export

Front end export excel, JS xlsx export

2022-06-24 09:07:39Secret Shura

It can be used directly table Label export , My is Vue + element Project

demand

Read many documents and cases to write a function , Because it was exported from the back-end , First front end export excel.

Environmental Science

My business needs are only for table The contents of the tag are exported to excel, So I can only write how to make a table The contents of the element are exported to excel.

git Link to download : https://codechina.csdn.net/mirrors/SheetJS/js-xlsx?utm_source=csdn_github_accelerator.

npm Install the following

npm install xlsx

Go straight to the code , After downloading import To page

<template>
    <div style="margin:20px">
        <div >&lt;  return </div>
        <div > Budget table </div>
        <table id="table1" class="myClass_style" border="1" cellspacing="0"  cellpadding="0" >
            <thead>
                <tr id="theadName" style="height:40px;font-weight:600;background-color:#EEFD6F;text-align:center">
                </tr>
            </thead>
            <tbody>
                <tr id="theadContent" >
                    <td>{
    {
    item.my2Arr1}}</td>
                    <td style="background:#A7F0EF">1111</td>
                    <td style="background:#A7F0EF">2222</td>
                </tr>
            </tbody>
        </table>
        <button class="myClassButton" @click="toPreActivities()"> export </button>
    </div>
</template>

<script>
    import XLSX from 'xlsx/xlsx.mini'   //  export excel Plug in import for  
    import {
    openDownloadDialog,sheet2blob} from 'js/export.js '  // Remember to import here openDownloadDialog Method 
    export default {
    
    data(){
    
        return{
    
           }
    },
    mounted(){
    
            this.showTheList()
    },
    methods:{
    
          //  export excel
            toPreActivities(){
    
                var table1 = document.querySelector("#table1");
                var sheet = XLSX.utils.table_to_sheet(table1);// Will a table The object is converted to a sheet object 
                exportPage.openDownloadDialog(sheet2blob(sheet),'wo.xlsx');
            },
    }
}
</script>

 Insert picture description here
openDownloadDialog Here's how , Remember to import it , If not, it won't work , Show undefined 了

//*** File to use when exporting **** */ 
import XLSX from 'xlsx/xlsx.mini';
//  Will a sheet Turn into the final excel Of documents blob object , And then use it URL.createObjectURL download 
export  function sheet2blob(sheet, sheetName) {
    
    sheetName = sheetName || 'sheet1';
    var workbook = {
    
        SheetNames: [sheetName],
        Sheets: {
    }
    };
    workbook.Sheets[sheetName] = sheet; //  Generate excel Configuration item for 

    var wopts = {
    
        bookType: 'xlsx', //  The type of file to generate 
        bookSST: false, //  Whether to generate Shared String Table, The official explanation is , If you turn on generate speed will decrease , But in the lower version IOS Better compatibility on the device 
        type: 'binary'
    };
    var wbout = XLSX.write(workbook, wopts);
    var blob = new Blob([s2ab(wbout)], {
    
        type: "application/octet-stream"
    }); //  String rotation ArrayBuffer
    function s2ab(s) {
    
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
    return blob;
}
export  function openDownloadDialog(url, saveName) {
    
    if (typeof url == 'object' && url instanceof Blob) {
    
        url = URL.createObjectURL(url); //  establish blob Address 
    }
    var aLink = document.createElement('a');
    aLink.href = url;
    aLink.download = saveName || ''; // HTML5 New properties , Specify the save file name , No suffixes , Be careful ,file:/// It doesn't work in mode 
    var event;
    if (window.MouseEvent) event = new MouseEvent('click');
    else {
    
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    }
    aLink.dispatchEvent(event);
}
//******* */ 

Finally, it turned out to be here , If you need other styles, you have to change the source code yourself Or see method 2 , The author writes in great detail .
 Insert picture description here

Method 2 :https://www.cnblogs.com/liuxianan/p/js-excel.html Click here , Sure

copyright notice
author[Secret Shura],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240625104923.html

Random recommended