Skip to content

Commit 68a5e13

Browse files
committed
Merge pull request #51 from carrbrpoa/export/xls
Adds the possibility to export to XLS (Office 2004 and below)
2 parents e01af19 + 4db3bbe commit 68a5e13

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

widgets/Export.js

+69
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ define([
5050

5151
excel: true, // allow attributes to be exported to Excel
5252
csv: true, // allow attributes to be exported to CSV
53+
xlsExcel: true, // allow attributes to be exported to Excel (XLS format)
5354

5455
// spatial exports are not ready
5556
//geojson: false, // allow features to be exported to GeoJSON
@@ -96,6 +97,7 @@ define([
9697
var options = [];
9798
var exportOptions = [
9899
{value: 'excel', label: i18n.exportToExcel, type: 'attributes'},
100+
{value: 'xlsExcel', label: i18n.exportToXlsExcel, type: 'attributes'},
99101
{value: 'csv', label: i18n.exportToCSV, type: 'attributes'},
100102
{value: 'geojson', label: i18n.exportToGeoJSON, type: 'features'},
101103
{value: 'shapefile', label: i18n.exportToShapeFile, type: 'features'}
@@ -137,6 +139,9 @@ define([
137139
if (typeof(options.csv) !== 'undefined') {
138140
this.csv = options.csv;
139141
}
142+
if (options.xlsExcel !== 'undefined') {
143+
this.xlsExcel = options.xlsExcel;
144+
}
140145
/*
141146
if (options.geojson !== undefined) {
142147
this.geojson = options.geojson;
@@ -179,6 +184,9 @@ define([
179184
case 'shapefile':
180185
this.exportToShapeFile();
181186
break;
187+
case 'xlsExcel':
188+
this.exportToXLS();
189+
break;
182190
default:
183191
break;
184192
}
@@ -188,6 +196,67 @@ define([
188196
* Excel/CSV Functions
189197
*******************************/
190198

199+
exportToXLS: function () {
200+
var xlsContents = this.buildXLSContents();
201+
if (!xlsContents) {
202+
topic.publish('viewer/handleError', {
203+
widget: 'Export',
204+
error: '${i18n.errorExcel}'
205+
});
206+
return;
207+
}
208+
209+
// To UTF-8
210+
var uint8 = new Uint8Array(xlsContents.length);
211+
for (var i = 0; i < uint8.length; i++) {
212+
uint8[i] = xlsContents.charCodeAt(i);
213+
}
214+
215+
this.downloadFile(uint8, 'application/vnd.ms-excel', 'results.xls', true);
216+
},
217+
218+
buildXLSContents: function () {
219+
var separator = '\t';
220+
var carriageReturn = '\r';
221+
var rows = this.grid.get('store').data;
222+
var columns = this.grid.get('columns');
223+
224+
// Prepare formatted columns
225+
var formattedColumns = columns.map(function (column) {
226+
if (column.exportable !== false && column.hidden !== true) {
227+
return column.label || column.field;
228+
}
229+
});
230+
231+
var formattedRows = [];
232+
233+
// Prepare rows' contents
234+
array.forEach(rows, function (row) {
235+
var formattedRow = [];
236+
237+
array.forEach(columns, function (column) {
238+
if (column.exportable !== false && column.hidden !== true) {
239+
var field = column.field;
240+
var val = row[field];
241+
242+
if (column.get) {
243+
val = column.get(row);
244+
}
245+
if (val === null || val === 'undefined') {
246+
formattedRow.push('');
247+
return;
248+
}
249+
250+
formattedRow.push(val.toString());
251+
}
252+
});
253+
254+
formattedRows.push(formattedRow.join(separator));
255+
});
256+
257+
return formattedColumns.join(separator) + carriageReturn + formattedRows.join(carriageReturn);
258+
},
259+
191260
exportToXLSX: function () {
192261
var ws = this.createXLSX();
193262
if (!ws) {

widgets/Export/nls/Export.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ define({
1414
'errorExcel': 'Could not create Excel Spreadsheet',
1515
'errorCSV': 'Could not create CSV File',
1616
'errorGeoJSON': 'Could not create GeoJSON File',
17-
'errorShapeFile': 'Could not create Shapefile'
17+
'errorShapeFile': 'Could not create Shapefile',
18+
'exportToXlsExcel': 'Export To Excel (XLS)'
1819
}
1920
});

0 commit comments

Comments
 (0)