【问题标题】:WP_List_Table export data to CSV formatWP_List_Table 将数据导出为 CSV 格式
【发布时间】:2015-12-08 05:53:41
【问题描述】:

我正在使用WP_List_Table 类在后端创建一个从myCustomTable 获取记录的表。是否有任何功能/插件可用于将单行和多行导出为CSV 格式。我尝试了很多插件,但他们都想出了整个表格导出。

谢谢

【问题讨论】:

    标签: php wordpress export-to-csv


    【解决方案1】:

    是的,你可以做到。这是我的代码示例,我用它来获取数据库表到 excel 格式。您只需根据需要替换数据库连接、表、表字段和查询,此代码可能会对您有所帮助

    <?php
    include '../common/inc.common.php';//db connectivity 
    
    $class=$_REQUEST['class_id'];//get the class id
    $line .= "\n";
    $filename='../app/class_export_student_hsc.csv';// file name
    $fp = fopen($filename, "w");
    
    $sql="SELECT t1 . * ,y.aca_year
    FROM (
    SELECT  name,fathername,school_name,edu_disctrict,revenue_disctrict,nationality,religion,caste,castetype,gender,dob,dobword,mark1,stat
    FROM **myCustomTable**
    )t1, academic_years y
    WHERE t1.academic_year=y.refid
    AND t1.class =$class
    ";
    
    
    $studentArray=$Cobj->run_query($sql);
    $line = "";
    foreach($studentArray as $name => $valuee) {
    $comma = "";
        foreach($valuee as $key2 => $value){
        $line .= $comma . '"' . str_replace('"', '""', $key2) . '"';
        $comma = ",";
    }
        $line .= "\n";
        break;
        }
    foreach($studentArray as $name => $valuee) {
    $comma = "";
        foreach($valuee as $key2 => $value){
        $line .= $comma . '"' . str_replace('"', '""', $value) . '"';
        $comma = ",";
        }   
        $line .= "\n";
        }
    fputs($fp, $line);
    fclose($fp);
    if (file_exists($filename)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename="'.basename($filename).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($filename));
        readfile($filename);
        exit;
    }
    ?>
    

    【讨论】:

    • 你这里用的是WP_LIST_TABLE吗
    • @VasimVanzara:不,它是一个简单的 php 代码,用于将表格导出到 csv。
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    相关资源
    最近更新 更多