首页 > Web开发 > 详细

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

时间:2017-01-07 00:06:25      阅读:371      评论:0      收藏:0      [点我收藏+]
<!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> 

代码来自互联网

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

原文:http://www.cnblogs.com/subendong/p/6257477.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!