Skip to content

Instantly share code, notes, and snippets.

@muhammedfurkan
Forked from bbachi/ExportCSV.js
Created February 3, 2023 11:43
Show Gist options
  • Save muhammedfurkan/1e0d8237780216771fd2ef94de7d0572 to your computer and use it in GitHub Desktop.
Save muhammedfurkan/1e0d8237780216771fd2ef94de7d0572 to your computer and use it in GitHub Desktop.
react excel functionality
import React from 'react'
import Button from 'react-bootstrap/Button';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
export const ExportCSV = ({csvData, fileName}) => {
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const fileExtension = '.xlsx';
const exportToCSV = (csvData, fileName) => {
const ws = XLSX.utils.json_to_sheet(csvData);
const wb = { Sheets: { 'data': ws }, SheetNames: ['data'] };
const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
const data = new Blob([excelBuffer], {type: fileType});
FileSaver.saveAs(data, fileName + fileExtension);
}
return (
<Button variant="warning" onClick={(e) => exportToCSV(csvData,fileName)}>Export</Button>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment