This page covers a non-latest version of Handsontable.
Export to CSV
Overview The plugin allows you to export data from Handsontable into a CSV file.
Live examples Export to file
Export as a JavaScript Blob object Open a console in browser developer tools to see the result for the below example.
import { useRef, useEffect } from 'react';
import Handsontable from 'handsontable';
import ReactDOM from 'react-dom';
import { HotTable } from '@handsontable/react';
import { registerAllModules } from 'handsontable/registry';
import 'handsontable/dist/handsontable.full.min.css';
// register Handsontable's modules
registerAllModules();
const ExampleComponent = () => {
const hotRef = useRef(null);
let buttonClickCallback;
useEffect(() => {
const hot = hotRef.current.hotInstance;
const exportPlugin = hot.getPlugin('exportFile');
buttonClickCallback = () => {
const exportedBlob = exportPlugin.exportAsBlob('csv', {
bom: false,
columnDelimiter: ',',
columnHeaders: false,
exportHiddenColumns: true,
exportHiddenRows: true,
mimeType: 'text/csv',
rowDelimiter: '\r\n',
rowHeaders: true
});
console.log(exportedBlob);
};
});
return (
<>
<HotTable
ref={hotRef}
data={Handsontable.helper.createSpreadsheetData(7, 7)}
colHeaders={true}
rowHeaders={true}
hiddenRows={{ rows: [1, 3, 5], indicators: true }}
hiddenColumns={{ columns: [1, 3, 5], indicators: true }}
height="auto"
licenseKey="non-commercial-and-evaluation"
/>
<div className="controls">
<button id="export-blob" onClick={(...args) => buttonClickCallback(...args)}>Export as a Blob</button>
</div>
</>
);
};
ReactDOM.render(<ExampleComponent />, document.getElementById('example2'));
<script src="https://cdn.jsdelivr.net/npm/handsontable@12.1/dist/handsontable.full.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/handsontable@12.1/dist/handsontable.full.min.css" />
<script src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@handsontable/react@12.1/dist/react-handsontable.js"></script>
<script src="https://handsontable.com/docs/12.1/scripts/fixer.js"></script>
<script src="https://handsontable.com/docs/12.1/scripts/helpers.js"></script>
<div id="example2" class="hot "></div>
Edit in JSFiddle
Export as a string Open a console in browser developer tools to see the result for the below example.
import { useRef, useEffect } from 'react';
import Handsontable from 'handsontable';
import ReactDOM from 'react-dom';
import { HotTable } from '@handsontable/react';
import { registerAllModules } from 'handsontable/registry';
import 'handsontable/dist/handsontable.full.min.css';
// register Handsontable's modules
registerAllModules();
const ExampleComponent = () => {
const hotRef = useRef(null);
let buttonClickCallback;
useEffect(() => {
const hot = hotRef.current.hotInstance;
const exportPlugin = hot.getPlugin('exportFile');
buttonClickCallback = () => {
const exportedString = exportPlugin.exportAsString('csv', {
bom: false,
columnDelimiter: ',',
columnHeaders: false,
exportHiddenColumns: true,
exportHiddenRows: true,
rowDelimiter: '\r\n',
rowHeaders: true
});
console.log(exportedString);
};
});
return (
<>
<HotTable
ref={hotRef}
data={Handsontable.helper.createSpreadsheetData(7, 7)}
colHeaders={true}
rowHeaders={true}
hiddenRows={{ rows: [1, 3, 5], indicators: true }}
hiddenColumns={{ columns: [1, 3, 5], indicators: true }}
height="auto"
licenseKey="non-commercial-and-evaluation"
/>
<div className="controls">
<button id="export-string" onClick={(...args) => buttonClickCallback(...args)}>Export as a string</button>
</div>
</>
);
};
ReactDOM.render(<ExampleComponent />, document.getElementById('example3'));
<script src="https://cdn.jsdelivr.net/npm/handsontable@12.1/dist/handsontable.full.min.js"></script>
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/handsontable@12.1/dist/handsontable.full.min.css" />
<script src="https://cdn.jsdelivr.net/npm/react@17/umd/react.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@handsontable/react@12.1/dist/react-handsontable.js"></script>
<script src="https://handsontable.com/docs/12.1/scripts/fixer.js"></script>
<script src="https://handsontable.com/docs/12.1/scripts/helpers.js"></script>
<div id="example3" class="hot "></div>
Edit in JSFiddle
Available methods
TIP
To use the Handsontable API, you'll need access to the Handsontable instance. You can do that by utilizing a reference to the HotTable component, and reading its hotInstance property.
For more information, see the Instance Methods page.
The plugin exposes the following methods to export data.
All of them accept the same arguments:
This is required to prepare a predefined settings object. We currently allow for only 'csv' to be used.
options Object This is an optional argument. It contains a set of supported options and extends the predefined CSV configuration. For the complete list of options that you can use, see available options .
Available options in the export configuration Below you can find all supported options:
bom Boolean Allows you to export data with a BOM signature.
Please note: This property will prepend content with the UTF-16BE BOM signature (FE FF ). The browser will convert the signature to the UTF-8 value (EF BB BF ) automatically.
You can use this property in all of the available methods .
Default value: true
columnDelimiter String Allows you to define the columns delimiter.
You can use this property in all of the available methods .
Default value: ','
columnHeaders Boolean When set to true, includes column headers in the exported data.
You can use this property in all of the available methods .
The columnHeaders option doesn't support the NestedHeaders plugin .
Default value: false
exportHiddenColumns Boolean Allows you to export data from hidden columns.
You can use this property in all of the available methods .
Default value: false
exportHiddenRows Boolean Allows you to export data from hidden rows.
You can use this property in all of the available methods .
Default value: false
fileExtension String Allows you to define the file extension.
You can use this property in the downloadFile method.
Default value: 'csv'
filename String Allows you to define the file name.
You can use predefined placeholders, which will be replaced by the date.
You can use this property in the downloadFile method.
Default value: 'Handsontable [YYYY]-[MM]-[DD]'
mimeType String Allows you to define the MIME type.
You can use this property in the downloadFile and exportAsBlob methods.
Default value: 'text/csv'
range Array Allows you to define a range of dataset to export. It's represented by an array of numeric, visual indexes [startRow, startColumn, endRow, endColumn].
You can use this property in all of the available methods .
Default value: 'text/csv'
rowDelimiter String Allows you to define rows delimiter.
You can use this property in all of the available methods .
Default value: '\r\n'
Allows you to export data with their row header.
You can use this property in all of the available methods .
Default value: false