UNPKG

@pilag6/csv-downloader

Version:

Lightweight CSV export utility for Vue and React

135 lines (100 loc) โ€ข 3.05 kB
# ๐Ÿ“„ CSV Downloader [![npm version](https://img.shields.io/npm/v/@pilag6/csv-downloader.svg?color=blue&label=npm)](https://www.npmjs.com/package/@pilag6/csv-downloader) [![npm downloads](https://img.shields.io/npm/dm/@pilag6/csv-downloader.svg?color=green)](https://www.npmjs.com/package/@pilag6/csv-downloader) [![license](https://img.shields.io/npm/l/@pilag6/csv-downloader.svg)](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.