There are a lot of Excel tables in the Nodejs template, and here's a brief introduction to the use of a module I used.
First, install the Excel module first:
NPM Install Node-xlsx
Then, introduce the module in your 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: {Tablei
D: "xxx",//53eacbe4e4b0693fbf5fd13b key: "XXX"}, 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 = 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 value var defaultsetting = {//if returned false can cancel this request Beforesend:function (req) {}, Complete:function (req) {}, DAT
A: ',//Object, String dataType: ' JSON ', Error:function () {}, headers: {},//{k:v, ...}
StatusCode: {}, Success:function (data) {}, Timeout:10, type: ' Get ',//Get, POST URL: ' www.baidu.com '}; /** */function Ajax (Settings) {//Ajaxlbs.js (Settings) if (typeof settings = = "Object") {//handle default value inheritance//TODO
For (key in defaultsetting) {if (settings[key] = = null) {Settings[key] = Defaultsetting[key];
}} var params = Url.parse (Settings.url, true); Params parsed parameters as follows//{//"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, 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 = httpunity.request (options,function (res) {var data = ';
Res.on (' Data ', function (chunk) {data + = chunk;
The. On (' End '), function () {if (Settings.datatype = = "json") {try {data = json.parse (data);
catch (e) {data = null;
} settings.success (data);
Settings.complete (req);
});
). 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);
} req.settimeout (Settings.timeout);
Req.end ();
} Exports.ajax = Ajax;
Generated Excel content:
SOURCE Download: Nodejs get network data and generate Excel form
The above is the entire content of this article, I hope to help you learn.