function Export()
{
global $wpdb;
// Use headers so the data goes to a file and not displayed
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="export.csv"');
// clean out other output buffers
ob_end_clean();
$fp = fopen('php://output', 'w');
// CSV/Excel header label
$header_row = array(
0 => 'Email Address',
1 => 'First Name',
2 => 'Last Name',
);
//write the header
fputcsv($fp, $header_row);
// retrieve any table data desired. Members is an example
$Table_Name = $wpdb->prefix.'members';
$sql_query = $wpdb->prepare("SELECT * FROM $Table_Name", 1) ;
$rows = $wpdb->get_results($sql_query, ARRAY_A);
if(!empty($rows))
{
foreach($rows as $Record)
{
$OutputRecord = array($Record['Email'],
$Record['FirstName'],
$Record['LastName']);
fputcsv($fp, $OutputRecord);
}
}
fclose( $fp );
exit; // Stop any more exporting to the file
}
2条答案
按热度按时间vxf3dgd41#
试试这个,
希望这对你有帮助。
更多信息,
wordpress脚本,用于执行杂项数据的.csv导出
将wordpress数据库表导出到excel
导出到csv wordpress
wpx232ag2#
我的解决方案。它们的关键是清除ob缓存。相应地更改表和字段类型。