Demo - Export to CSV
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.grapecity.com/wijmo/5.latest/styles/wijmo.min.css">
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.input.min.js"></script>
<script src="https://cdn.grapecity.com/wijmo/5.latest/controls/wijmo.grid.filter.min.js"></script>
<div class="container">
<h1>
Export to CSV
</h1>
<p>
To export a FlexGrid to CSV, call the
<b>FlexGrid.getClipString</b> method to get a string
representing the whole grid or any given range,
then the string to a file:</p>
<button id="btnExportGrid" class="btn btn-default">
Export Grid to CSV
</button>
<button id="btnExportSelection" class="btn btn-default">
Export Selection to CSV
</button>
<div id="theGrid"></div>
</div>
CSS
.wj-flexgrid {
max-height: 250px;
margin: 12px 0;
}
body {
margin-bottom: 20px;
}
JavaScript
onload = function() {
// create some random data
var countries = 'US,Germany,UK,Japan,Italy,Greece'.split(',');
var data = [];
for (var i = 0; i < 200; i++) {
data.push({
id: i,
country: countries[i % countries.length],
sales: Math.random() * 10000,
expenses: Math.random() * 5000
});
}
// show the data in a grid
var theGrid = new wijmo.grid.FlexGrid('#theGrid', {
autoGenerateColumns: false,
columns: [{
binding: 'id',
header: 'ID',
width: 60
},
{
binding: 'country',
header: 'Country',
width: '2*'
},
{
binding: 'sales',
header: 'Sales',
width: '*',
format: 'n2'
},
{
binding: 'expenses',
header: 'Expenses',
width: '*',
format: 'n2'
}
],
itemsSource: data
});
// add a filter to show that it works while exporting
var f = new wijmo.grid.filter.FlexGridFilter(theGrid);
// export grid or selection to CSV
document.getElementById('btnExportGrid').addEventListener('click', function() {
var rng = new wijmo.grid.CellRange(0, 0, theGrid.rows.length - 1, theGrid.columns.length - 1),
csv = theGrid.getClipString(rng, true, true);
exportFile(csv, 'FlexGrid.csv');
});
document.getElementById('btnExportSelection').addEventListener('click', function() {
var rng = theGrid.selection,
csv = theGrid.getClipString(rng, true, true);
exportFile(csv, 'Selection.csv');
});
function exportFile(csv, fileName) {
var fileType = 'txt/csv;charset=utf-8';
if (navigator.msSaveBlob) { // IE
navigator.msSaveBlob(new Blob([csv], {
type: fileType
}), fileName);
} else {
var e = document.createElement('a');
e.setAttribute('href', 'data:' + fileType + ',' + encodeURIComponent(csv));
e.setAttribute('download', fileName);
e.style.display = 'none';
document.body.appendChild(e);
...