@pilag6/csv-downloader
Version:
Lightweight CSV export utility for Vue and React
135 lines (100 loc) โข 3.05 kB
Markdown
# ๐ CSV Downloader
[](https://www.npmjs.com/package/@pilag6/csv-downloader)
[](https://www.npmjs.com/package/@pilag6/csv-downloader)
[](LICENSE)
**CSV Downloader** is a lightweight and fast utility to export data as CSV directly from the browser.
It works with **Vue**, **React**, and any JavaScript or TypeScript project.
## ๐ Installation
```bash
npm install @pilag6/csv-downloader
```
## ๐ฆ Basic usage
### Vue 3
```vue
<script setup lang="ts">
import { downloadCSV } from '@pilag6/csv-downloader';
const exportData = () => {
downloadCSV({
headers: ['Name', 'Age', 'Country'],
contents: [
['John Doe', 30, 'USA'],
['Jane Smith', 25, 'UK']
]
});
};
</script>
<template>
<button @click="exportData">Export CSV</button>
</template>
```
### React
```tsx
import React from 'react';
import { downloadCSV } from '@pilag6/csv-downloader';
export default function App() {
const exportData = () => {
downloadCSV({
headers: ['Name', 'Age', 'Country'],
contents: [
['John Doe', 30, 'USA'],
['Jane Smith', 25, 'UK']
],
filename: 'users.csv'
});
};
return <button onClick={exportData}>Export CSV</button>;
}
```
## โ๏ธ Options
| Option | Type | Default | Description |
|------------|------------------------|-------------|-------------|
| `headers` | `string[]` | **Required**| Column names |
| `contents` | `(string\|number)[][]` | **Required**| Data in rows and columns |
| `filename` | `string` | `data.csv` | CSV file name |
| `separator`| `string` | `,` | Column separator |
| `bom` | `boolean` | `true` | Add UTF-8 BOM for Excel compatibility |
## ๐งช Advanced example
```ts
import { downloadCSV } from '@pilag6/csv-downloader';
downloadCSV({
headers: ['Product', 'Quantity', 'Price (โฌ)'],
contents: [
['T-Shirt', 3, 19.99],
['Pants', 1, 39.5],
['Shoes', 2, 59.0]
],
filename: 'inventory.csv',
separator: ';', // Use semicolon as separator
bom: true // Excel compatible
});
```
## ๐ก Features
โ
Works with Vue, React, and plain JavaScript projects
โ
Supports any separator (`;`, `,`, `\t`, etc.)
โ
Automatically escapes double quotes
โ
Optional BOM for Excel compatibility
โ
Lightweight, zero dependencies
## ๐ฅ Quick install & run
```bash
npm install @pilag6/csv-downloader
```
**Import and use:**
```ts
import { downloadCSV } from '@pilag6/csv-downloader';
downloadCSV({
headers: ['A', 'B', 'C'],
contents: [['1', '2', '3']]
});
```
## ๐ Support
Found a bug or have a suggestion? Open an issue on GitHub or contact me.
## ๐ License
MIT โ Free for personal and commercial use.