• 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";');
        }



  • 相关阅读:
    Compiere中的树
    Compiere 模型构建
    LSMW魔鬼教程
    Compiere 因翻译工作没有完成,所以现在系统中的所有帮助去掉
    插入、更新扩展字段
    SAPscript Forms 教程
    SAP ABAP 效率测试
    批量更新数据表
    月份的描述表T247
    SAP 程序下载工具
  • 原文地址:https://www.cnblogs.com/JasonLeemz/p/5068174.html
Copyright © 2020-2023  润新知