WP_List_Table将数据导出为CSV格式

时间:2015-12-08 05:53:41

标签: php wordpress wordpress-plugin export-to-csv

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

由于

1 个答案:

答案 0 :(得分:0)

是的,你可以做到。这是我的代码的一个例子,我用它来获得数据库表格excel formate。您只需根据需要替换数据库连接,表,表字段和查询,此代码可以帮助您

<?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;
}
?>