Skip to content

Commit

Permalink
feat(Excel export): Excel export (#6199)
Browse files Browse the repository at this point in the history
* Initial checkin of export to excel. No examples or unit tests

* Eliminate $$hashKey and uidPrefix = 'uiGrid' from export

* Sheet name cannot be empty string or it causes export errors. Default to 'Sheet1'

* Additional languages

* Add documentation

* fix syntax causing lint errors. Check if not a tree when exporting to prevent error

* Updated example with colors

* Update unit tests and fix the lodash version for import to remove ambiguity.
  • Loading branch information
monster910 authored and mportuga committed Nov 15, 2017
1 parent 97e0ffa commit dc7d773
Show file tree
Hide file tree
Showing 19 changed files with 557 additions and 12 deletions.
13 changes: 11 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,21 @@
"test",
"tests"
],
"main": ["./ui-grid.css", "./ui-grid.js"],
"main": [
"./ui-grid.css",
"./ui-grid.js"
],
"devDependencies": {
"google-code-prettify": "~1.0.2",
"jquery": "~1.8.0",
"lodash": "~2.4.1",
"pdfmake": "~0.1.8",
"csv-js": "https://github.com/c0bra/CSV-JS.git#~1.0.2"
"csv-js": "https://github.com/c0bra/CSV-JS.git#~1.0.2",
"excel-builder-js": "excelbuilder#^2.0.2",
"jszip": "~2.6.1"
},
"resolutions": {
"jszip": "2.6.1",
"lodash": "~2.4.1"
}
}
5 changes: 4 additions & 1 deletion grunt/ngdocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ module.exports = {
'//ajax.googleapis.com/ajax/libs/angularjs/<%= latestAngular %>/angular-animate.js',
'bower_components/csv-js/csv.js',
'bower_components/pdfmake/build/pdfmake.js',
'bower_components/pdfmake/build/vfs_fonts.js'
'bower_components/pdfmake/build/vfs_fonts.js',
'bower_components/lodash/dist/lodash.min.js',
'bower_components/jszip/dist/jszip.min.js',
'bower_components/excel-builder-js/dist/excel-builder.dist.js'
],
hiddenScripts: [
'//ajax.googleapis.com/ajax/libs/angularjs/<%= latestAngular %>/angular-animate.js',
Expand Down
8 changes: 8 additions & 0 deletions misc/tutorial/206_exporting_data.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ directive on your grid. If you want to export as PDF you need to have installed
available through:
<pre> bower install pdfmake </pre>

If you want to export as Excel you need to have installed Excel-Builder,
available through:
<pre> bower install lodash
bower install jszip#2.6.1
bower install excelbuilder </pre>

The options and API for exporter can be found at {@link api/ui.grid.exporter ui.grid.exporter}, and

- {@link api/ui.grid.exporter.api:ColumnDef columnDef}
Expand Down Expand Up @@ -73,6 +79,8 @@ In this example we use the native grid menu buttons, and we show both the pdf an
exporterPdfPageSize: 'LETTER',
exporterPdfMaxGridWidth: 500,
exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
exporterExcelFilename: 'myFile.xlsx',
exporterExcelSheetName: 'Sheet1',
onRegisterApi: function(gridApi){
$scope.gridApi = gridApi;
}
Expand Down
152 changes: 152 additions & 0 deletions misc/tutorial/410_excel_exporting_data_and_header.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
@ngdoc overview
@name Tutorial: 410 Exporting Data with Fonts, Colors and Header in Excel
@description

<div class="alert alert-success" role="alert"><strong>Stable</strong> This feature is stable. There should no longer be breaking api changes without a deprecation warning.</div>

The exporter feature allows data to be exported from the grid in
excel format with a header. The exporter can export all data, visible data or selected data.

To use the exporter you need to include the ui-grid-exporter directive on
your grid. If you want to export selected rows you must include the ui-grid-selection
directive on your grid.

If you want to export as Excel you need to have installed Excel-Builder which also uses lodash and jszip,
available through:
<pre> bower install lodash
bower install jszip#2.6.1
bower install excelbuilder </pre>

The options and API for exporter can be found at {@link api/ui.grid.exporter ui.grid.exporter}, and

- {@link api/ui.grid.exporter.api:ColumnDef columnDef}
- {@link api/ui.grid.exporter.api:GridOptions gridOptions}
- {@link api/ui.grid.exporter.api:PublicApi publicApi}

The exporter adds menu items to the grid menu, to use the native UI you need to enable
the grid menu using the gridOption `enableGridMenu`

Note that the option to export selected data is only visible if you have data selected.

Additionally, information about the Excel Builder may be found at https://github.com/stephenliberty/excel-builder.js.

If the example below we show how to make a custom header based on a callback method and


@example
In this example we use the native grid menu buttons, and we show the pdf, csv and excel export options.

<example module="app">
<file name="app.js">
var app = angular.module('app', ['ngAnimate', 'ngTouch', 'ui.grid', 'ui.grid.selection', 'ui.grid.exporter']);

app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.formatters = {};

$scope.gridOptions = {
columnDefs: [
{ field: 'name' },
{ field: 'gender', visible: false},
{ field: 'company' }
],
enableGridMenu: true,
enableSelectAll: true,
exporterCsvFilename: 'myFile.csv',
exporterPdfDefaultStyle: {fontSize: 9},
exporterPdfTableStyle: {margin: [30, 30, 30, 30]},
exporterPdfTableHeaderStyle: {fontSize: 10, bold: true, italics: true, color: 'red'},
exporterPdfHeader: { text: "My Header", style: 'headerStyle' },
exporterPdfFooter: function ( currentPage, pageCount ) {
return { text: currentPage.toString() + ' of ' + pageCount.toString(), style: 'footerStyle' };
},
exporterPdfCustomFormatter: function ( docDefinition ) {
docDefinition.styles.headerStyle = { fontSize: 22, bold: true };
docDefinition.styles.footerStyle = { fontSize: 10, bold: true };
return docDefinition;
},
exporterPdfOrientation: 'portrait',
exporterPdfPageSize: 'LETTER',
exporterPdfMaxGridWidth: 500,
exporterCsvLinkElement: angular.element(document.querySelectorAll(".custom-csv-link-location")),
exporterExcelFilename: 'myFile.xlsx',
exporterExcelSheetName: 'Sheet1',
exporterExcelCustomFormatters: function ( grid, workbook, docDefinition ) {

var stylesheet = workbook.getStyleSheet();
var aFormatDefn = {
"font": { "size": 9, "fontName": "Calibri", "bold": true },
"alignment": { "wrapText": true }
};
var formatter = stylesheet.createFormat(aFormatDefn);
// save the formatter
$scope.formatters['bold'] = formatter;

aFormatDefn = {
"font": { "size": 9, "fontName": "Calibri" },
"fill": { "type": "pattern", "patternType": "solid", "fgColor": "FFFFC7CE" },
"alignment": { "wrapText": true }
};
formatter = stylesheet.createFormat(aFormatDefn);
// save the formatter
$scope.formatters['red'] = formatter;

Object.assign(docDefinition.styles , $scope.formatters);

return docDefinition;
},
exporterExcelHeader: function (grid, workbook, sheet, docDefinition) {
// this can be defined outside this method
var stylesheet = workbook.getStyleSheet();
var aFormatDefn = {
"font": { "size": 9, "fontName": "Calibri", "bold": true },
"alignment": { "wrapText": true }
};
var formatterId = stylesheet.createFormat(aFormatDefn);

sheet.mergeCells('B1', 'C1');
var cols = [];
cols.push({ value: '' });
cols.push({ value: 'My header that is long enough to wrap', metadata: {style: formatterId.id} });
sheet.data.push(cols);
},
exporterFieldFormatCallback: function(grid, row, gridCol, cellValue) {
// set metadata on export data to set format id. See exportExcelHeader config above for example of creating
// a formatter and obtaining the id
var formatterId = null;
if (cellValue && cellValue.startsWith('W')) {
formatterId = $scope.formatters['red'].id;
}

if (formatterId) {
return {metadata: {style: formatterId}};
} else {
return null;
}
},
onRegisterApi: function(gridApi){
$scope.gridApi = gridApi;
}
};

$http.get('/data/100.json')
.success(function(data) {
$scope.gridOptions.data = data;
});

}]);
</file>

<file name="index.html">
<div ng-controller="MainCtrl">
<div ui-grid="gridOptions" ui-grid-selection ui-grid-exporter class="grid"></div>
</div>
</file>

<file name="main.css">
.grid {
width: 500px;
height: 400px;
}
</file>
</example>
Loading

0 comments on commit dc7d773

Please sign in to comment.