Vue 3 export to Excel: download JSON as an .xlsx file
vue3-json-to-excel is a small Vue 3 component that renders a link and downloads your data as an .xlsx file when it is clicked. The workbook is built in the browser with SheetJS, so there is no server endpoint to write.
Install and register
npm i vue vue3-json-to-excel
Import it in the component that needs the button:
<script setup>
import Vue3JsonToExcel from 'vue3-json-to-excel';
const rows = [
{ id: 1, name: 'Ada', city: 'London' },
{ id: 2, name: 'Grace', city: 'New York' },
];
</script>
<template>
<vue3-json-to-excel :json-data="rows" file-name="people" button-text="Export to Excel" />
</template>
Or register it once for the whole app, since the default export has an install() method:
import { createApp } from 'vue';
import Vue3JsonToExcel from 'vue3-json-to-excel';
import App from './App.vue';
createApp(App).use(Vue3JsonToExcel).mount('#app');
Props
jsonData(required): one object, which gives one row, or an array of objects, one row each.fileName: the name without extension. The default isoutput, and.xlsxis added.buttonText: the link text. The default isDownload.
The component renders a single <a> element, so a class you put on it lands on
the link. That is how you make it look like the buttons in your design system.
Export what the user sees
jsonData is read when the link is clicked, so pass a computed value and the file always matches
the current filter or sort:
<script setup>
import { computed, ref } from 'vue';
import Vue3JsonToExcel from 'vue3-json-to-excel';
const props = defineProps({ orders: Array });
const onlyPaid = ref(false);
const visible = computed(() => (onlyPaid.value ? props.orders.filter((o) => o.paid) : props.orders));
const fileName = computed(() => `orders-${new Date().toISOString().slice(0, 10)}`);
</script>
<template>
<label><input v-model="onlyPaid" type="checkbox" /> Paid only</label>
<vue3-json-to-excel class="btn" :json-data="visible" :file-name="fileName" button-text="Export to Excel" />
</template>
Map the rows before passing them if the export needs different headers than your API fields, for example
visible.map((o) => ({ Order: o.id, Customer: o.customer })).
Nested data and versions
The current source flattens nested objects into dotted columns such as shipping.city and writes
arrays to one cell as JSON text. That needs a release newer than 1.0.5: version 1.0.5 on npm writes one flat
object as a single row. Until a newer release is published, flatten the rows yourself or use the code below.
Without the component
The component is a thin wrapper. If you need more control, such as several sheets or column widths, call SheetJS from a method:
import * as XLSX from 'xlsx';
function exportOrders(rows) {
const workbook = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(workbook, XLSX.utils.json_to_sheet(rows), 'Orders');
XLSX.writeFile(workbook, 'orders.xlsx');
}
JSON to Excel in JavaScript covers column order, widths and dates. To try the component without writing code, the JSON to Excel converter on this site is built on it.