当前位置 博文首页 > 文章内容

    功能模块 js json转csv json格式的数据生成为csv文件

    作者: 栏目:未分类 时间:2020-10-29 18:00:32

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    原文:
    https://www.cnblogs.com/xiyangbaixue/p/4210278.html

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
    
    
        <div align="center">
            <h3><u>Enter JSON data</u></h3>
            <div class='mydiv'>
                <textarea id="txt" class='txtarea' rows="15" cols="100">[{"Vehicle":"BMW","Date":"30 Jul 2013 09:24 AM","Location":"Hauz Khas","Speed":42},{"Vehicle":"Honda CBR","Date":"30 Jul 2013 12:00 AM","Location":"Military Road","Speed":0},{"Vehicle":"Supra","Date":"30 Jul 2013 07:53 AM","Location":"Sec-45","Speed":58},{"Vehicle":"Land Cruiser","Date":"30 Jul 2013 09:35 AM","Location":"DLF Phase I","Speed":83}]</textarea>
            </div>
            <br />
            <button class="download">Download CSV</button>
        </div>
    
    
    
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <script type="text/javascript">
    
    
            // 原文:https://www.cnblogs.com/xiyangbaixue/p/4210278.html
    
    
            $(function () {
    
                var d = [
                    { "Vehicle": "BMW", "Date": "30 Jul 2013 09:24 AM", "Location": "Hauz Khas", "Speed": 42 },
                    { "Vehicle": "Honda CBR", "Date": "30 Jul 2013 12:00 AM", "Location": "Military Road", "Speed": 0 },
                    { "Vehicle": "Supra", "Date": "30 Jul 2013 07:53 AM", "Location": "Sec-45", "Speed": 58 },
                    { "Vehicle": "Land Cruiser", "Date": "30 Jul 2013 09:35 AM", "Location": "DLF Phase I", "Speed": 83 }
                ];
    
    
                d = [{ "platformOrderSn": "12044000000", "orderCreateTime": "2020-09-25 13:22:29", "title": "测试0", "memberUsername": "用户0", "orderStatus": 0, "payAmount": "348.00" }, { "platformOrderSn": "12044000001", "orderCreateTime": "2020-09-25 13:22:29", "title": "测试1", "memberUsername": "用户1", "orderStatus": 0, "payAmount": "348.00" }, { "platformOrderSn": "12044000002", "orderCreateTime": "2020-09-25 13:22:29", "title": "测试2", "memberUsername": "用户2", "orderStatus": 0, "payAmount": "348.00" }, { "platformOrderSn": "12044000003", "orderCreateTime": "2020-09-25 13:22:29", "title": "测试3", "memberUsername": "用户3", "orderStatus": 0, "payAmount": "348.00" }, { "platformOrderSn": "12044000004", "orderCreateTime": "2020-09-25 13:22:29", "title": "测试4", "memberUsername": "用户4", "orderStatus": 0, "payAmount": "348.00" }];
    
    
    
                var msieversion = function () {
                    var ua = window.navigator.userAgent;
                    var msie = ua.indexOf("MSIE ");
                    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
                    {
                        return true;
                    } else { // If another browser,
                        return false;
                    }
                    return false;
                };
    
    
                var JSONToCSVConvertor = function (JSONData, ShowLabel) {
                    var arrData = typeof JSONData !== 'object' ? JSON.parse(JSONData) : JSONData;
                    var CSV = '';
                    if (ShowLabel) {
                        var row = "";
                        for (var index in arrData[0]) {
                            row += index + ',';
                        }
                        row = row.slice(0, -1);
                        CSV += row + '\r\n';
                    }
                    for (var i = 0; i < arrData.length; i++) {
                        var row = "";
                        for (var index in arrData[i]) {
                            var arrValue = arrData[i][index] == null ? "" : '="' + arrData[i][index] + '"';
                            row += arrValue + ',';
                        }
                        row.slice(0, row.length - 1);
                        CSV += row + '\r\n';
                    }
                    if (CSV == '') {
                        growl.error("Invalid data");
                        return;
                    }
                    var fileName = "Result";
                    if (msieversion()) {
                        var IEwindow = window.open();
                        IEwindow.document.write('sep=,\r\n' + CSV);
                        IEwindow.document.close();
                        IEwindow.document.execCommand('SaveAs', true, fileName + ".csv");
                        IEwindow.close();
                    } else {
                        //var uri = 'data:application/csv;charset=utf-8,' + escape(CSV);
                        var uri = 'data:application/csv;charset=utf-8,' + CSV;
                        var link = document.createElement("a");
                        link.href = uri;
                        link.style = "visibility:hidden";
                        link.download = fileName + ".csv";
                        document.body.appendChild(link);
                        link.click();
                        document.body.removeChild(link);
                    }
                };
    
    
                JSONToCSVConvertor(d, true);
    
            });
    
    
        </script>
    </body>
    </html>