Skip to content

Instantly share code, notes, and snippets.

@bbachi
Created June 30, 2019 19:14
Show Gist options
  • Save bbachi/87119ac6b839aa265cbf616c3ebe607b to your computer and use it in GitHub Desktop.
Save bbachi/87119ac6b839aa265cbf616c3ebe607b 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