JSON数据转Excel node.js实现KEY,value转换



var fs = require('fs');
var express = require('express');
var bodyParser = require('body-parser');
var nodeExcel = require('excel-export');

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.get('/', function (req, res) {
fs.readFile('./index.html', function (err, data) {
if (err) {
res.writeHead(404, {
'Content-Type': 'text/html'
});
} else {
res.writeHead(200, {
'Content-Type': 'text/html'
});
res.write(data.toString());
}
res.end();
});
});

app.post('/exportExcel', function (req, res) {
var arrayData = [];
var conf = {};
conf.name = 'mysheet';
conf.cols = [{
caption: 'KEY',
type: 'string',
}, {
caption: 'Value',
type: 'string',
}];

var jsonData = eval('(' + req.body.jsonData + ')');
var i = 0;
for (var key in jsonData) {
arrayData = [key.toString(), jsonData[key].toString()];
i++
}
conf.rows = arrayData;
var result = nodeExcel.execute(conf);
res.setHeader('Content-Type', 'application/vnd.openxmlformats');
res.setHeader('Content-Disposition', 'attachment; filename=' + 'exportExcel.xlsx');
res.end(result, 'binary');
});

app.listen(3000);
console.log('listen:http://127.0.0.1:3000');


<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>JSON数据导出Excel</title>
<style type="text/css">
body {
color: #000;
}

* {
margin: 0;
padding: 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
}

.main-cnb {
width: 1000px;
margin: 20px auto;
}

.main-cnb .title {
font-size: 30px;
line-height: 50px;
text-align: center;
}

.main-cnb .fm-c {
padding: 20px 0 0 0;
}

.main-cnb .fm-c dl {
margin-top: 20px;
width: 100%;
}

.main-cnb .fm-c dl dd {
padding: 15px 0 0 0;
text-align: left;
}

.main-cnb .fm-c .jsonData {
width: 100%;
line-height: 20px;
float: left;
padding: 5px 10px;
}

.main-cnb .fm-c .ipt-submit {
margin-top: 20px;
width: 100%;
line-height: 50px;
height: 50px;
background: #f39800;
border: 1px solid #f39800;
border-radius: 5px;
font-size: 20px;
font-weight: bold;
color: #fff;
cursor: pointer;
}

.clearfix {
zoom: 1;
}

.clearfix:after {
content: ".";
display: block;
height: 0;
visibility: hidden;
clear: both;
}
</style>
</head>

<body>
<form action="http://127.0.0.1:3000/exportExcel" method="post" οnsubmit="return chkForm(this);">
<div class="main-cnb">
<div class="title">Excel导出</div>
<div class="fm-c">
<dl>
<dt>JSON数据</dt>
<dd class="clearfix">
<textarea class="jsonData" id="jsonData" name="jsonData" rows="20"></textarea>
</dd>
</dl>
<dl>
<input class="ipt-submit" type="submit" value="提 交"></input>
</dl>
</div>
</div>
</form>

<script type="text/javascript">
function isJSON(str) {
if (typeof str == 'string') {
try {
var obj = JSON.parse(str);
if (typeof obj == 'object' && obj) {
return true;
} else {
return false;
}
} catch (e) {
console.log('error:' + str + '!!!' + e);
return false;
}
}
}

function chkForm(obj) {
var jsonData = obj.jsonData.value;
if (jsonData == '') {
alert('请输入JSON数据!');
return false;
}
if (isJSON(jsonData)) {
return true;
} else {
alert('非法JSON格式!');
return false;
}
}
</script>
</body>

</html>


转载于:https://juejin.im/post/5c7283dde51d457ead58cc6b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值