大家好,这里介绍一下xls转json的方法。
1、直接用XLs工具可以转出Json(这个就不介绍了)。
2、用nodejs程序执行上面的json转换操作,我用的NOdeJS版本是4.2.2,下面贴一下全全部代码:
//文件上传 //获取当前时间(年月日时分秒) function getnow() { var date = new Date(); var seperator1 = ""; var seperator2 = ""; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + "" + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds()+date.getMilliseconds(); return currentdate; } //服务开始 var express = require(‘express‘); var app = express(); var fs = require("fs"); var http = require(‘http‘); var $ = require("jquery"); var xlsx = require("node-xlsx");// use be json var json=null; var bodyParser = require(‘body-parser‘); var multer = require(‘multer‘); app.use(express.static(‘public‘)); app.use(bodyParser.urlencoded({ extended: false })); app.use(multer({ dest: ‘/tmp/‘}).array(‘image‘)); app.get(‘/index.html‘, function (req, res) { res.sendFile( __dirname + "/" + "index.html" ); }); app.post(‘/file_upload‘, function (req, res) { var namelast=getnow()+ req.files[0].originalname; var des_file = __dirname + "/files/" + namelast; fs.readFile( req.files[0].path, function (err, data) { //300M文件限制 if(314572800<data.length) { response = { code:15, msg:‘file length is max‘, data:"", path:"", name:req.files[0].originalname }; res.end( JSON.stringify( response ) ); } else{ //写入文件 fs.writeFile(des_file, data, function (err) { if( err ){ console.log( err ); }else{ json = xlsx.parse(des_file); response = { code:0, msg:‘File uploaded successfully‘, data:JSON.stringify(json), path:namelast, name:req.files[0].originalname }; fs.writeFile("json.json", JSON.stringify(json),function (err) { if (err) throw err ; }); } res.end( JSON.stringify( json ) ); }); } }); }); var server = app.listen(3000, function () { console.log("address: http://localhost:3000"); });
备用地址/demo:http://182.92.155.97:3000/index.html
使用方法:打开地址,点击Upload上传按钮即可选择*.xls文件上传后、Json数据直接显示在页面、当然也可【CTRL+A】全选JSON:
END
时间: 2024-09-26 23:12:34