Skip to content

Exporting Content to Excel File

To export content as an Excel file in Vue.js, follow these steps:

  1. Table Configuration: Prepare your content structure within a designated container, assigning it a specific ID for export.

    html
    <template>
        <div>
            <table id="exportTable">
                <!-- Your table or content structure -->
                <!-- ... -->
            </table>
            <ExportToExcel element="exportTable" filename="document">
                <button>Click here to export</button>
            </ExportToExcel>
        </div>
    </template>
  2. Utilize ExportToExcel Component: Incorporate the ExportToExcel component to encapsulate your content and enable Excel export functionality.

    javascript
    <script lang="ts">
    import { ExportToExcel } from 'vue-doc-exporter';
    
    export default {
        components: {
            ExportToExcel
        }
    }
    </script>

Export JSON Excel

Exports JSON data in various formats (XLS, CSV, HTML) with support for multiple sheets, custom headers/footers, and other features.

Component Props

PropTypeDefaultDescription
typestring'xls'The file format for the export. Options: 'xls', 'csv', 'html'.
data`any[]any[][]`[]
fieldsRecord<string, any>{}Mapping of field names to export fields.
exportFieldsRecord<string, any>{}Alternative field mapping for export.
defaultValuestring''Default value for missing data.
header`stringstring[]`undefined
footer`stringstring[]`undefined
namestring'data.xls'The name of the exported file.
fetch() => Promise<any[]>undefinedFunction to fetch data if not provided in data.
metaany[][]Metadata to include in the export.
sheetsstring[][]Names of the sheets for multi-sheet exports.
worksheetstring'Sheet1'Name of the default worksheet.
beforeGenerate() => Promise<void>undefinedCallback function before generation.
beforeFinish() => Promise<void>undefinedCallback function before finishing the export.
escapeCsvbooleantrueWhether to escape CSV values.
stringifyLongNumbooleanfalseWhether to stringify long numbers.

Usage Example

html
<template>
  <DataExport
    type="csv"
    :data="myData"
    :fields="myFields"
    :header="['Custom Header']"
    :footer="['Custom Footer']"
    name="my_data.csv"
    @click="handleClick"
  />
</template>

<script setup lang="ts">
import { DataExport } from 'vue-doc-exporter' // Path to the component
import { ref } from 'vue';

const myData = ref([...]); // Your data here
const myFields = ref({...}); // Your fields mapping here

const handleClick = () => {
  
};
</script>