forked from adazzle/react-data-grid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
POC: Export to CSV, XLSX, PDF (adazzle#2355)
* Initial commit * Fix dependencies * Add utility function to export * serialiseCellValue * Export to pdf and xlsx * Update src/hooks/useViewportColumns.ts Co-authored-by: Nicolas Stepien <[email protected]> * Update src/hooks/useViewportRows.ts Co-authored-by: Nicolas Stepien <[email protected]> * Update stories/demos/exportUtils.tsx Co-authored-by: Nicolas Stepien <[email protected]> * Update stories/demos/exportUtils.tsx Co-authored-by: Nicolas Stepien <[email protected]> Co-authored-by: Nicolas Stepien <[email protected]>
- Loading branch information
1 parent
4f8f521
commit 5bfe24b
Showing
6 changed files
with
150 additions
and
7 deletions.
There are no files selected for viewing
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { cloneElement } from 'react'; | ||
import type { ReactElement } from 'react'; | ||
import { renderToStaticMarkup } from 'react-dom/server'; | ||
import XLSX from 'xlsx'; | ||
import { jsPDF } from 'jspdf'; | ||
import autoTable from 'jspdf-autotable'; | ||
|
||
import type { DataGridProps } from '../../src'; | ||
|
||
export function exportToCsv<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>, fileName: string) { | ||
const { head, body, foot } = getGridContent(gridElement); | ||
const content = [...head, ...body, ...foot] | ||
.map(cells => cells.map(serialiseCellValue).join(',')) | ||
.join('\n'); | ||
|
||
downloadFile(fileName, new Blob([content], { type: 'text/csv;charset=utf-8;' })); | ||
} | ||
|
||
export function exportToXlsx<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>, fileName: string) { | ||
const { head, body, foot } = getGridContent(gridElement); | ||
const wb = XLSX.utils.book_new(); | ||
const ws = XLSX.utils.aoa_to_sheet([...head, ...body, ...foot]); | ||
XLSX.utils.book_append_sheet(wb, ws, 'Sheet 1'); | ||
XLSX.writeFile(wb, fileName); | ||
} | ||
|
||
|
||
export function exportToPdf<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>, fileName: string) { | ||
const doc = new jsPDF({ | ||
orientation: 'l', | ||
unit: 'px' | ||
}); | ||
|
||
const { head, body, foot } = getGridContent(gridElement); | ||
autoTable(doc, { | ||
head, | ||
body, | ||
foot, | ||
horizontalPageBreak: true, | ||
styles: { cellPadding: 1.5, fontSize: 8, cellWidth: 'wrap' }, | ||
tableWidth: 'wrap' | ||
}); | ||
doc.save(fileName); | ||
} | ||
|
||
function getGridContent<R, SR>(gridElement: ReactElement<DataGridProps<R, SR>>) { | ||
const grid = document.createElement('div'); | ||
grid.innerHTML = renderToStaticMarkup(cloneElement(gridElement, { | ||
enableVirtualization: false | ||
})); | ||
|
||
return { | ||
head: getRows('.rdg-header-row'), | ||
body: getRows('.rdg-row:not(.rdg-summary-row)'), | ||
foot: getRows('.rdg-summary-row') | ||
}; | ||
|
||
function getRows(selector: string) { | ||
return Array.from( | ||
grid.querySelectorAll<HTMLDivElement>(selector) | ||
).map(gridRow => { | ||
return Array.from( | ||
gridRow.querySelectorAll<HTMLDivElement>('.rdg-cell') | ||
).map(gridCell => gridCell.innerText); | ||
}); | ||
} | ||
} | ||
|
||
function serialiseCellValue(value: unknown) { | ||
if (typeof value === 'string') { | ||
const formattedValue = value.replace(/"/g, '""'); | ||
return formattedValue.includes(',') ? `"${formattedValue}"` : formattedValue; | ||
} | ||
return value; | ||
} | ||
|
||
function downloadFile(fileName: string, data: Blob) { | ||
const downloadLink = document.createElement('a'); | ||
downloadLink.download = fileName; | ||
const url = URL.createObjectURL(data); | ||
downloadLink.href = url; | ||
downloadLink.click(); | ||
URL.revokeObjectURL(url); | ||
} |