• js导出表格数据


    考虑到浏览器兼容性问题,采用原生js和后台交互下载网页数据

    js:

    var table = $('.table-panel table');
                // Header
                var tdData ="";
                $(table).find('thead').find('tr').each(function() {
                    $(this).find('th').each(function() {
                        tdData += '"' + $(this).text() + '",';
                    });
                    tdData = $.trim(tdData).substring(0, tdData.length -1);
                    tdData += "
    ";
                });
    
                // Row vs Column
                $(table).find('tbody').find('tr').each(function() {
                    $(this).find('td').each(function(index,data) {
                        tdData += '"'+ $(this).text() + '",';
                    });
                    tdData = $.trim(tdData).substring(0, tdData.length -1);
                    tdData += "
    ";
                });
                
                var form=$("<form>");//定义一个form表单
                form.attr("style","display:none");
                form.attr("target","");
                form.attr("method","post");
                form.attr("action","/api/data/exportCSV");
                var input1=$("<input>");
                input1.attr("type","hidden");
                input1.attr("name","exportData");
                input1.attr("value",tdData);
                $("body").append(form);//将表单放置在web中
                form.append(input1);
                form.submit();//表单提交


    PHP:

    public function exportCSV() {
            $exportData = $_POST['exportData'];
            $exportData = iconv("UTF-8", "GB18030//IGNORE", $exportData);
            // open raw memory as file so no temp files needed, you might run out of memory though
            $f = fopen('php://output', 'w');
            fwrite($f, $exportData);
            // tell the browser it's going to be a csv file
            header('Content-Type: application/csv');
            // tell the browser we want to save it instead of displaying it
            header('Content-Disposition: attachment; filename="exportData.csv";');
        }



  • 相关阅读:
    Js中的正则表达式
    js内存泄露的几种情况
    JavaScript的setTimeout与setInterval执行时机
    IE模拟addDOMLoadEvent和jQuery的ready实现
    谈谈JavaScript的异步实现
    在iOS7中修改状态栏字体的颜色
    IOS 疯狂基础之 页面间跳转
    ATL2.1版的CString分析
    翻译: 如何改变MFC应用程序主窗口的类名
    VC5.0中的ATL的一个有趣的bug
  • 原文地址:https://www.cnblogs.com/JasonLeemz/p/5068174.html
Copyright © 2020-2023  润新知