您的位置:首页 > Web前端 > JavaScript

拷贝excel里的内容转为JSON的js代码

2017-01-06 19:13 453 查看
<!DOCTYPE html>
<html lang="en">
<head>
<title>excel转json</title>
<meta charset="utf-8">
</head>
<body>
<textarea id="content" rows="20" cols="100"></textarea>
<input type="button" value="转换" id="btnChange" onclick="ctyperow()" />
<textarea id="result" rows="20" cols="100"></textarea>

<input type="hidden" id="ctype" value="0" />

<script src="http://cdn.bootcss.com/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
var fgf = /\t/;
function ctyperow() {
fgf = $("#fgfstr").val() || fgf;
var ctype = $("#ctype").val();
var txt = $("#content").val();
var datas = txt.split("\n");
var html = "[\n";
var keys = [];
for (var i = 0; i < datas.length; i++) {
var ds = datas[i].split(fgf);
if (i == 0) {
if (ctype == "0") {
keys = ds;
} else {
html += "[";
for (var j = 0; j < ds.length; j++) {
html += '"' + ds[j] + '"';
if (j < ds.length - 1) {
html += ",";
}
}
html += "],\n";
}
} else {
if (ds.length == 0)
continue;
if (ds.length == 1) {
ds[0] == "";
continue;
}
html += ctype == "0" ? "{" : "[";
for (var j = 0; j < ds.length; j++) {
var d = ds[j];
if (d == "")
continue;
if (ctype == "0") {
html += '"' + keys[j] + '":"' + d + '"';
} else {
html += '"' + d + '"';
}
if (j < ds.length - 1) {
html += ',';
}
}
html += ctype == "0" ? "}" : "]";
if (i < datas.length - 1)
html += ",\n";
}
}
html += "\n]";
$("#result").val(html);
}
</script>
</body>
</html>


代码来自互联网
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: