<!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>
代码来自互联网