How to export data to CSV in Vue

Exporting data to CSV format is essential for Vue applications that handle tables, reports, and data analytics features. As the creator of CoreUI with over 11 years of Vue development experience since 2014, I’ve built CSV export functionality into countless dashboards and admin panels. The most effective solution is to convert your data array to CSV format and trigger a download using blob URLs. This approach works entirely client-side without requiring backend processing.

Convert data to CSV format and trigger download in Vue 3.

<template>
  <button @click='exportToCSV'>Export to CSV</button>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const data = ref([
      { id: 1, name: 'John Doe', email: '[email protected]' },
      { id: 2, name: 'Jane Smith', email: '[email protected]' },
      { id: 3, name: 'Bob Johnson', email: '[email protected]' }
    ])

    const exportToCSV = () => {
      const headers = Object.keys(data.value[0]).join(',')
      const rows = data.value.map(row =>
        Object.values(row).join(',')
      ).join('\n')

      const csv = `${headers}\n${rows}`
      const blob = new Blob([csv], { type: 'text/csv' })
      const url = window.URL.createObjectURL(blob)

      const link = document.createElement('a')
      link.href = url
      link.download = 'export.csv'
      link.click()

      window.URL.revokeObjectURL(url)
    }

    return { exportToCSV }
  }
}
</script>

The CSV export function extracts headers from the first object’s keys, then maps each data row to comma-separated values. A blob is created with CSV content and the appropriate MIME type. A temporary anchor element triggers the download with the specified filename. The blob URL is revoked after download to prevent memory leaks.

Best Practice Note

This is the same CSV export approach we use in CoreUI Vue tables and data grids. For production use, handle edge cases like commas or quotes in data by wrapping values in quotes and escaping internal quotes. Consider using a library like PapaParse for complex CSV generation with proper escaping and encoding.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Convert a Map to an Array in JavaScript
How to Convert a Map to an Array in JavaScript

How to migrate CoreUI React Templates to Vite
How to migrate CoreUI React Templates to Vite

How to Remove Underline from Link in CSS
How to Remove Underline from Link in CSS

How to check if an array is empty in JavaScript?
How to check if an array is empty in JavaScript?

Answers by CoreUI Core Team