Created
June 30, 2019 19:14
-
-
Save bbachi/87119ac6b839aa265cbf616c3ebe607b to your computer and use it in GitHub Desktop.
react excel functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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