我有此脚本可将自定义管理表导出到CSV文件。 除为空的“参考”列外,它都能正常运行。
mysql查询为“ Ref”列输出正确的值(0001、0002、0003 ...),但是在CSV文件中该列为空?你知道原因吗?
add_action('wp_ajax_csv_pull','csv_pull_wpse_212972');
function csv_pull_wpse_212972() {
$arrayStatus = array(0 => 'Libero', 1 => 'Prenotato', 2 => 'Pagato');
global $wpdb;
$table_name = $wpdb->prefix . 'wlist_transactions';
$tblCustomers = $wpdb->prefix . 'wlist_customers';
$tblSpouses = $wpdb->prefix . 'wlist_spouses';
$sql = "SELECT $table_name.ID,
$table_name.Ref AS Referenza,
$table_name.PaymentMethod AS MetodoPagamento,
$table_name.PaymentDate AS DataPagamento,
$table_name.Amount AS Totale,
$table_name.Status,
$tblCustomers.Customer as Cliente,
$tblSpouses.Names as Spousi,
$tblSpouses.Data
FROM $table_name
left join $tblCustomers on $table_name.Customer = $tblCustomers.ID
left join $tblSpouses on $tblCustomers.ID_Spouses = $tblSpouses.ID";
$file = 'lista_transazioni';
$results = $wpdb->get_results($sql, ARRAY_A);
if (empty($results)) {
return;
}
$csv_output = '"'.implode('";"',array_keys($results[0])).'";'."\n";;
foreach ($results as $row) {
$row['Status'] = $arrayStatus[$row['Status']];
$csv_output .= '"'.implode('";"', $row).'";'."\n";
}
$csv_output .= "\n";
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
}
答案 0 :(得分:0)
该列似乎包含前导零的数字。为了在excel中正确显示它们,您需要在它们前面加上'
所以尝试
foreach ($results as $row) {
$row[1] = '\''.(string)$row[1];
$row['Status'] = $arrayStatus[$row['Status']];
$csv_output .= '"'.implode('";"', $row).'";'."\n";
}
也许您不需要将它们强制转换为字符串。