Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with vue 3 #16

Open
the-spider-911 opened this issue Jun 22, 2021 · 2 comments
Open

Working with vue 3 #16

the-spider-911 opened this issue Jun 22, 2021 · 2 comments

Comments

@the-spider-911
Copy link

Does this work with Vue 3, i tried but its not showing the vue-excel-xlsx button

@dcoyt
Copy link

dcoyt commented Oct 29, 2021

I'm doing a plan for upgrading to Vue 3, is this working with Vue 3? Thanks!

@Waseem-Almoliky
Copy link

Waseem-Almoliky commented Apr 18, 2023

<template>
    <button @click="exportExcel()">
        <slot></slot>
    </button>
</template>

<script>
import { utils, writeFileXLSX } from 'xlsx';
export default {
    name: "vue-excel-xlsx",
    props: {
        columns: {
            type: Array,
            default: () => []
        },
        data: {
            type: Array,
            default: () => []
        },
        fileName: {
            type: String,
            default: 'excel'
        },
        sheetName: {
            type: String,
            default: 'SheetName'
        },
        fileType: {
            type: String,
            default: 'xlsx',
            validator: (val) => ['xlsx', 'xls'].includes(val)
        }
    },

    methods: {
        exportExcel() {
            console.log('run');
            let createXLSLFormatObj = [];
            let newXlsHeader = [];
            let vm = this;
            if (vm.columns.length === 0) {
                console.log("Add columns!");
                return;
            }
            if (vm.data.length === 0) {
                console.log("Add data!");
                return;
            }
            vm.columns.map(column => {
                newXlsHeader.push(column.label);
            });

            createXLSLFormatObj.push(newXlsHeader);
            vm.data.map(value => {
                let innerRowData = [];
                vm.columns.map(val => {
                    let fieldValue = value[val.field];
                    if (val.field.split('.').length > 1) {
                        fieldValue = this.getNestedValue(value, val.field);
                    }
                    if (val.dataFormat && typeof val.dataFormat === 'function') {
                        innerRowData.push(val.dataFormat(fieldValue));
                    } else {
                        innerRowData.push(fieldValue);
                    }
                });
                createXLSLFormatObj.push(innerRowData);
            });

            let fileName = vm.fileName + "." + vm.fileType;

            let ws_name = vm.sheetName;

            let wb = utils.book_new(),
                ws = utils.aoa_to_sheet(createXLSLFormatObj);
            utils.book_append_sheet(wb, ws, ws_name);
            writeFileXLSX(wb, fileName);
        },
        getNestedValue(object, string) {
            string = string.replace(/\[(\w+)\]/g, '.$1');
            string = string.replace(/^\./, '');
            let a = string.split('.');
            for (let i = 0, n = a.length; i < n; ++i) {
                let k = a[i];
                if (k in object) {
                    object = object[k];
                } else {
                    return;
                }
            }
            return object;
        }
    }
}
</script>

use this component for now till the repo gets updated. note I used "xlsx": "^0.18.5"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants