Async Version: http://blog.csdn.net/zzwwjjdj1/article/details/52129192write this version is mainly, promise more useful, and, now is the nodejs of the built-in objects, no need to reference third-party library-The tools neededMySQL Database
Connection database module MySQL Basic package: http://blog.csdn.net/zzwwjjdj1/article/details/51991348
Auto-run module Node-schedule basic use: http://blog.csdn.net/zzwwjjdj1/article/details/51898257
Generate Excel Table Node-xlsx
Send mail Nodemailer basic use: http://blog.csdn.net/zzwwjjdj1/article/details/51878392
--Installation ModuleI set up a folder on the F drive: Automation
NPM install MySQL
NPM Install Node-schedule
NPM Install Node-xlsx
NPM Install Nodemailer
--Encapsulation of 3 methods1. Accessing the database
2. Send mail
3. Format time
Promise.js
/* Tool Class August 22, 2016 09:07:50 */var db = {}; var mysql = require (' mysql '); var Nodemailer = require (' Nodemailer '); var pool = Mysql.createpool ({connectionlimit:10, host: ' Local Host ', User: ' Root ', password: ' 123456 ', database: ' Nodejs '}); Executes the SQL statement and returns the result db.query = function (SQL) {return new Promise (function (resolve, reject) {if (!sql) {Reject ( ' Pass parameter Error! '); Return } pool.query (SQL, function (err, rows, fields) {if (err) {Console.log (err); Reject (err) return; }; Resolve (rows); }); })};//send mail with attachment Db.sendmail = function (xlsxname) {var transporter = Nodemailer.createtransport ({service: ' QQ ', a Uth: {User: ' [email protected] ', pass: ' Drckgvaniifuxxxx '}}); var mailoptions = {from: ' [email protected] ', to: ' [email protected] ', Subject: ' Boss, you want Excel to come, format yourself Deal with it! ', HTML: ' --Test codeAuto.jsvar xlsx = require (' node-xlsx '), var schedule = require ("Node-schedule"), var mysql = require ('./promise '); var fs = require ( ' FS '); var auto = function () {mysql//queries the data and translates to the format required to generate xlsx. Query (' SELECT * from Users '). Then (function (rows) {var datas = [] Rows.foreach (function (row) {var newRow = [];for (var key in row) {Newrow.push (Row[key]);} Datas.push (NewRow);}) Return Promise.resolve (datas);}) Generate xlsx file. Then (function (datas) {var buffer = Xlsx.build ([{name: "Today's revenue", Data:datas}]); var xlsxname = ' ${mysql.nowdate (). Split (') [0]}.xlsx '; return new Promise (function (resolve, reject) {fs.writefile (xlsxname, buffer, ' binary ', function (Err) {if (err) {throw new error (' Create Excel exception '); return;} Resolve (Xlsxname)})})//Send message, return information. Then (function (xlsxname) {return mysql.sendmail (xlsxname);}). Then (function (info) {Console.log (info);}) Catches unhandled exceptions. catch (function (e) {console.log (e);}); var rule = new schedule. Recurrencerule (); var t = [];for (var i = 0; i <; i++) {T.push (i);} var times = T;rule.second = Times;schedulE.schedulejob (rule, function () {Auto ();});
--runto see the effect, the automatic execution is set to 1 seconds to execute once.
-- mail--
-- specific content- -
-- printing information--
--Database--
--excel--
Nodejs implementation, automatic daily reading of database data-Generate Excel form-Send to Boss Mailbox (promise version)