Nodejs obtains network data and generates an Excel table named nodejsexcel.
The node. js template contains many Excel tables. Here I will briefly introduce the use of a module I have used.
First, install the Excel module:
npm install node-xlsx
Then, introduce the module in the Code:
var xlsx = require('node-xlsx');
Finally, get the data and write it into 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" }, 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);}
Ajax code:
Var https = require ("https"); var http = require ("http"); var Url = require ("url"); var querystring = require ('querystring '); // default value var defaultSetting = {// if false is returned, 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) {}, timeout: 10, type: 'get', // GET, POST url: "www.baidu.com"};/*****/function ajax (settings) {// ajaxlbs. js (settings) if (typeof settings = "object") {// process default value inheritance // todo... for (key in defaultSetting) {if (settings [key] = null) {settings [key] = defaultSetting [key] ;}} var params = Url. parse (settings. url, true); // params parses the following parameters: // {// "protocol": "http:", // "slashes": true, // "host": "localhost: 3000", // "p Ort ":" 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 = httpUnity. request (options, function (res) {var data = ''; res. on ('data', function (chunk) {data + = chunk ;}). 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:
Code: http://download.csdn.net/detail/qxs965266509/8896431
If you have reprinted please famous from http://blog.csdn.net/qxs965266509
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.