There are a lot of modules in the
Nodejs that generate Excel tables, so let's take a look at Node-xlsx
First, install the Excel module:
npm install node-xlsx
and then, Introduce module in code:
var xlsx = require (' node-xlsx ');
Finally, get the data and write to Excel:
Var fs = require (' FS ');
Var xlsx = require (' node-xlsx ');
Var ajax = require ('./ajax.js ');
Start (); Function start () { ajax.ajax ({ url: "Http://yuntuapi.amap.com/datamanage/data/list", type: "Get", data: { tableid: "XXX",//53eacbe4e4b0693fbf5fd13b key: "XXX"
  &NBSP}, success: function (data) {
var myDatas = []; var datas = (JSON. Parse (data)). Datas;
var count = 0; for (var index in datas) {
var account = datas[index]; var colum
= []; var names
; if (Index == 0) {
names = []; } for (Var index2 in account) { if (index == 0)
names.push (INDEX2);
var value = account[index2]; if (value == null) { value = ""
; }
colum.push (value);
console.log (account); } if (index == 0) {
mydatas.push (names); }
mydatas.push (Colum); if (index == datas.length - 1) {
writexls (Mydatas); } }
console.log (mydatas.length);
} }); } function writexls (Datas) { var buffer = xlsx.build ({ worksheets: [ {"name": "Group", "Data":
Datas} ]};
fs.writefilesync ("group.csv", buffer, ' binary '); } function parsexls () {    VAR OBJ&NBsp;= xlsx.parse (' myfile.xlsx ');
console.log (obj); }
Code for the Ajax section:
Var https = require ("https");
Var http = require ("http");
Var url = require ("Url");
Var querystring = require (' querystring '); Default var defaultsetting = { // If you return false you can cancel this request beforeSend: function (req) { }, complete: function (req) { }, data: ', // object, string dataType: ' JSON ', error: function () { }, headers: {}, //
{k:v, .}
statuscode: {}, success: function (data) {    &NBSP}, timeout: 10, type: ' get ', // get, post
url: "Www.baidu.com"};
/** * */function ajax (settings) { // ajaxlbs.js (settings) if (typeof settings === "Object") {
// handling default values inherit // todo  .... for (key in defaultsetting) {
if (settings[key] == null) { settings[key]
= defaultSetting[key]; }  &NBSP} } var params = url.parse (
Settings.url, true); // params parse out the following parameters // { // "protocol ":" http: ", // " slashes ": true, // " host ":" LocalHost
: 3000 ", // " port ":" 3000 ", // " hostname ":" localhost ", // "href": "http://localhost:3000/?d=1", // "search": " ? d=1 ", // " query ": {" D ":" 1 "}, // " pathname ":"/", // "path": "/?d=1"     // } var options = { host: params.hostname, port: params.port | | 80, path: params.path, method: settings.type }; if (settings.data != null) { options.path += "?" for (var Key in settings.data) { options.path = options.path + "&" + key + "=" +
Settings.data[key]; } console.log (
Options.path);
} var httpUnity = http; if (params.protocol == "https:") {
options.port = 443;
var httpUnity = https; } var req =&nbSp;httpunity.request (options,function (res) { var
data = '; res.on (' Data ',function (chunk) {
data += chunk;        &NBSP}). On ("End", function () { if (settings.datatype === "JSON") { try { data =
Json.parse (data); } catch (e) {
data = null;
} }
Settings.success (data);
Settings.complete (req);
});    &NBSP}). On (' Error ', function (e) {
settings.error (e);
}); if (Typeof settings.beforesend === "function") {// if (! Settings.beforesend (req)) {//
Settings.complete (req);
req.end ();
return false; }// if (settings.type === "POST") { var datastr
= querystring.stringify (Settings.data);
req.setheader ("Content-length", dataStr.length);
req.write (DATASTR);
   &NBSP} req.settimeout (Settings.timeout);
req.end (); } exports.ajax = ajax;
Generated Excel content:
Node.js Export an Excel table instance with the Excel-export plug-in
Node.js you can easily export the required data to an Excel table as long as the Excel-export plugin is installed.
1, install the Excel-export plugin in the project directory
NPM Install Excel-export
2, page
<button id= "Exportexcel" class= "btn btn-warning" > Contract export </button>
js:$ ("#exportExcel"). Click (function () {
Console.info ("Exportexcel");
var id = $ ("#contractID"). Val ();
Console.info ("ID:" +id);
var url = "/api/contracts/exportexcel/" + ID;
Console.info (URL);
Window.location = url;//You cannot use the Get method to jump here, otherwise the download is unsuccessful
});
3,router.js
var nodeexcel = require (' Excel-export ');//Associated Excel-export module
var contract = require ('.. /app/controller/contract ');
App.get ('/api/contracts/exportexcel/:id ', contract.exportexcel);//jump to the background
4, backstage
Exports.exportexcel = function (req, res) {
/** static data
* var conf ={};
Conf.cols = [
{caption: ' String ', type: ' String '},
{caption: ' Date ', type: ' Date '},
{caption: ' bool ', type: ' BOOL '},
{caption: ' number ', type: ' Number '}
];
Conf.rows = [
[' Pi ', (New Date (2013, 4, 1)). Getjulian (), True, 3.14],
["E", (New Date (4, 1)). Getjulian (), False, 2.7182]
];
var result = Nodeexcel.execute (conf);
Res.setheader (' Content-type ', ' application/vnd.openxmlformats ');
Res.setheader ("Content-disposition", "attachment; Filename= "+" report.xlsx ");
Res.end (result, ' binary ');
**/
Console.log ("req.params.id:" +req.params.id);
var Queryid = req.params[' id ']+ ';
var contract = new contract ();
var conf = {};
Conf.cols = [
{caption: ' Purchase number ', type: ' String '},
{caption: ' Contract name ', type: ' String '},
{caption: ' Party a ', type: ' String '},
{caption: ' Party A department ', type: ' String '},
{caption: ' Party B ', type: ' String '},
{caption: ' B Department ', type: ' String '},
{caption: ' Signed date ', type: ' Date '},
{caption: ' Bid date ', type: ' Date '},
{caption: ' End Date ', type: ' Date '},
{caption: ' Sales owner ', type: ' String '},
{caption: ' Business owner ', type: ' String '},
{caption: ' Performance Attribution Department ', type: ' String '},
{caption: ' Amount ', type: ' Number '},
{caption: ' state ', type: ' String '}
];
var getId = {
_id:queryid
};
Contract.checkiddata (getid,function (data) {
Console.log ("Lijuanxia");
Console.log ("data.lentht" +data.length);
var m_data = [];
var arry = [Data[0].myid, Data[0].name, Data[0].partya, Data[0].partyadept, Data[0].partyb, Data[0].partybdept, data[0] SignDate, Data[0].begindate, Data[0].enddate, "sales Director", "Business director", "Performance Attribution Department", Data[0].amount, Data[0].state];
M_data[0] = Arry;
Conf.rows = m_data;
var result = Nodeexcel.execute (conf);
Res.setheader (' Content-type ', ' application/vnd.openxmlformats ');
Res.setheader ("Content-disposition", "attachment; Filename= "+data[0].name+". xlsx ");
Res.end (result, ' binary ');
});
}