@@ -50,6 +50,7 @@ define([
50
50
51
51
excel : true , // allow attributes to be exported to Excel
52
52
csv : true , // allow attributes to be exported to CSV
53
+ xlsExcel : true , // allow attributes to be exported to Excel (XLS format)
53
54
54
55
// spatial exports are not ready
55
56
//geojson: false, // allow features to be exported to GeoJSON
@@ -96,6 +97,7 @@ define([
96
97
var options = [ ] ;
97
98
var exportOptions = [
98
99
{ value : 'excel' , label : i18n . exportToExcel , type : 'attributes' } ,
100
+ { value : 'xlsExcel' , label : i18n . exportToXlsExcel , type : 'attributes' } ,
99
101
{ value : 'csv' , label : i18n . exportToCSV , type : 'attributes' } ,
100
102
{ value : 'geojson' , label : i18n . exportToGeoJSON , type : 'features' } ,
101
103
{ value : 'shapefile' , label : i18n . exportToShapeFile , type : 'features' }
@@ -137,6 +139,9 @@ define([
137
139
if ( typeof ( options . csv ) !== 'undefined' ) {
138
140
this . csv = options . csv ;
139
141
}
142
+ if ( options . xlsExcel !== 'undefined' ) {
143
+ this . xlsExcel = options . xlsExcel ;
144
+ }
140
145
/*
141
146
if (options.geojson !== undefined) {
142
147
this.geojson = options.geojson;
@@ -179,6 +184,9 @@ define([
179
184
case 'shapefile' :
180
185
this . exportToShapeFile ( ) ;
181
186
break ;
187
+ case 'xlsExcel' :
188
+ this . exportToXLS ( ) ;
189
+ break ;
182
190
default :
183
191
break ;
184
192
}
@@ -188,6 +196,67 @@ define([
188
196
* Excel/CSV Functions
189
197
*******************************/
190
198
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
+
191
260
exportToXLSX : function ( ) {
192
261
var ws = this . createXLSX ( ) ;
193
262
if ( ! ws ) {
0 commit comments