Skip to content

Remove global effects and node dependencies #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/example/node_modules
/node_modules
*xlsx*
20 changes: 17 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
# excel-export #

A simple node.js module for exporting data set to Excel xlsx file.
A simple js module for exporting data set to Excel xlsx file, it works in node and in the browser.

## Using excel-export ##
Setup configs object before passing it into the execute method. If generating multiple sheets, configs object can be an array of worksheet configuration. Or passing in a worksheet configuration to generate single worksheet xlsx file. Within a worksheet configuration uses **name** attribute to specify worksheet name. **cols** is an array for column definition. Column definition should have caption and type properties while width property is not required. The unit for width property is character. **beforeCellWrite** callback is optional. beforeCellWrite is invoked with row, cell data and option object (eOpt detail later) parameters. The return value from beforeCellWrite is what get written into the cell. Supported valid types are string, date, bool and number. **rows** is the data to be exported. It is an Array of Array (row). Each row should be the same length as cols. Styling is optional. However, if you want to style your spreadsheet, a valid excel styles xml file is needed. An easy way to get a styles xml file is to unzip an existing xlsx file which has the desired styles and copy out the styles.xml file. Use **stylesXmlFile** property of configuartion object to specify the relative path and file name of the xml file. Google for "spreadsheetml style" to learn more detail on styling spreadsheet. eOpt in beforeCellWrite callback contains rowNum for current row number. eOpt.styleIndex should be a valid zero based index from cellXfs tag of the selected styles xml file. eOpt.cellType is default to the type value specified in column definition. However, in some scenario you might want to change it for different format.
Setup configs object before passing it into the execute method. If generating multiple sheets, configs object can be an array of worksheet configuration. Or passing in a worksheet configuration to generate single worksheet xlsx file. Within a worksheet configuration uses **name** attribute to specify worksheet name. **cols** is an array for column definition. Column definition should have caption and type properties while width property is not required. The unit for width property is character. **beforeCellWrite** callback is optional. beforeCellWrite is invoked with row, cell data and option object (eOpt detail later) parameters. The return value from beforeCellWrite is what get written into the cell. Supported valid types are string, date, bool and number. **rows** is the data to be exported. It is an Array of Array (row). Each row should be the same length as cols. Styling is optional. However, if you want to style your spreadsheet, a valid excel styles xml file is needed. An easy way to get a styles xml file is to unzip an existing xlsx file which has the desired styles and copy out the styles.xml file. Use **stylesXml**
property of configuration object to specify the content of the xml file. Google for "spreadsheetml style" to learn more detail on styling spreadsheet. eOpt in beforeCellWrite callback contains rowNum for current row number. eOpt.styleIndex should be a valid zero based index from cellXfs tag of the selected styles xml file. eOpt.cellType is default to the type value specified in column definition. However, in some scenario you might want to change it for different format.



var express = require('express');
var nodeExcel = require('excel-export');
var app = express();
var fs = require('fs')

app.get('/Excel', function(req, res){
var conf ={};
conf.stylesXmlFile = "styles.xml";
var xmlFile = fs.readFileSync("styles.xml")
conf.stylesXml = xmlFile
conf.name = "mysheet";
conf.cols = [{
caption:'string',
Expand Down Expand Up @@ -62,3 +65,14 @@ Setup configs object before passing it into the execute method. If generating mu

app.listen(3000);
console.log('Listening on port 3000');

Easiest way to use it in the browser is using buffer. Do npm install buffer

var Buffer = require('buffer/').Buffer
// ... same as previous example
var result = nodeExcel.execute(conf)
var blob = new Blob([new Buffer(result, 'binary')], {type: 'application/vnd.openxmlformats'})
saveAs(blob, 'excel.xlsx')

### Disclaimer ###
This package is based in the [original](https://github.com/functionscope/Node-Excel-Export) but ported to support browser and avoid global side effects.
142 changes: 71 additions & 71 deletions example/app.js
Original file line number Diff line number Diff line change
@@ -1,86 +1,86 @@
var express = require('express'),
nodeExcel = require('excel-export'),
uuid = require('node-uuid'),
app = express();
nodeExcel = require('excel-export'),
uuid = require('node-uuid'),
app = express()

app.get('/Large', function(req, res){
var conf ={};
conf.cols = [];
for (i = 0; i < 100; i++){
app.get('/Large', function (req, res) {
var conf = {}
conf.cols = []
for (i = 0; i < 100; i++) {
conf.cols.push({
caption:'string ' + i,
captionStyleIndex: 1,
type:'string'
});
caption: 'string ' + i,
captionStyleIndex: 1,
type: 'string'
})
}
conf.rows = [];
for (j = 0; j < 1000; j++){
var row = [];
for (k = 0; k < 100; k++){
row.push(uuid.v4());
}
conf.rows.push(row);
conf.rows.push(row);
conf.rows = []
for (j = 0; j < 1000; j++) {
var row = []
for (k = 0; k < 100; k++) {
row.push(uuid.v4())
}
conf.rows.push(row)
conf.rows.push(row)
}
var result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Large.xlsx");
res.end(result, 'binary');
var result = nodeExcel.execute(conf)
res.setHeader('Content-Type', 'application/vnd.openxmlformats')
res.setHeader('Content-Disposition', 'attachment; filename=' + 'Large.xlsx')
res.end(result, 'binary')
})

});

app.get('/Excel', function(req, res){
var conf ={};
// uncomment it for style example
app.get('/Excel', function (req, res) {
var conf = {}
// uncomment it for style example
// conf.stylesXmlFile = "styles.xml";
conf.cols = [{
caption:'string',
captionStyleIndex: 1,
type:'string',
beforeCellWrite:function(row, cellData){
return cellData.toUpperCase();
}
, width:15
},{
caption:'date',
type:'date',
beforeCellWrite:function(){
var originDate = new Date(Date.UTC(1899,11,30));
return function(row, cellData, eOpt){
// uncomment it for style example
conf.cols = [{
caption: 'string',
captionStyleIndex: 1,
type: 'string',
beforeCellWrite: function (row, cellData) {
return cellData.toUpperCase()
},
width: 15
}, {
caption: 'date',
type: 'date',
beforeCellWrite: (function () {
var originDate = new Date(Date.UTC(1899, 11, 30))
return function (row, cellData, eOpt) {
// uncomment it for style example
// if (eOpt.rowNum%2){
// eOpt.styleIndex = 1;
// }
// }
// else{
// eOpt.styleIndex = 2;
// }
if (cellData === null){
eOpt.cellType = 'string';
return 'N/A';
} else
return (cellData - originDate) / (24 * 60 * 60 * 1000);
}
}()
, width:20.85
},{
caption:'bool',
type:'bool'
},{
caption:'number',
type:'number',
width:30
}];
conf.rows = [
if (cellData === null) {
eOpt.cellType = 'string'
return 'N/A'
} else {
return (cellData - originDate) / (24 * 60 * 60 * 1000)
}
}
}()),
width: 20.85
}, {
caption: 'bool',
type: 'bool'
}, {
caption: 'number',
type: 'number',
width: 30
}]
conf.rows = [
['pi', new Date(Date.UTC(2013, 4, 1)), true, 3.14159],
["e", new Date(2012, 4, 1), false, 2.7182],
['e', new Date(2012, 4, 1), false, 2.7182],
["M&M<>'", new Date(Date.UTC(2013, 6, 9)), false, 1.61803],
["null date", null, true, 1.414]
];
var result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
res.end(result, 'binary');
});
['null date', null, true, 1.414]
]
var result = nodeExcel.execute(conf)
res.setHeader('Content-Type', 'application/vnd.openxmlformats')
res.setHeader('Content-Disposition', 'attachment; filename=' + 'Report.xlsx')
res.end(result, 'binary')
})

app.listen(3000);
console.log('Listening on port 3000');
app.listen(3000)
console.log('Listening on port 3000')
175 changes: 78 additions & 97 deletions index.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"name": "excel-export",
"version": "0.5.1",
"description": "Simple data set export to Excel xlsx file",
"name": "js-excel",
"version": "1.0.0",
"description": "Data to excel in the browser and the server",
"main": "index.js",
"scripts": {
"test": "mocha test/main"
},
"repository": "https://github.com/functionscope/Node-Excel-Export",
"repository": "https://github.com/furstenheim/js-excel",
"keywords": [
"Excel",
"xlsx"
],
"author": "Ber-Lin Lai <[email protected]>",
"license": "BSD",
"dependencies": {
"collections": "^3.0.0",
"node-zip": "1.x"
"jszip": "2.5.0"
},
"devDependencies": {
"mocha": "",
"should": ""
"should": "",
"standard": "^8.6.0"
}
}
Loading