[英]Trying to export specific SQL data with leading 0 using excel mime
1 [這是輸出]試圖通過循環數組將SQL數據導出為excel文件,但未能導出數據的前導0。
我曾試圖將單引號內插,但仍然無法正常工作
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");
$user_query = mysqli_query($conn,$sql);
//echo $user_query;
// Write data to file
$flag = false;
while ($row = mysqli_fetch_assoc($user_query)) {
if (!$flag) {
// display field/column names as first row
echo implode("\t", array_keys($row)) . "\r\n";
$flag = true;
}
echo implode("\t", =array_values($row)) . "\r\n";
}
期望用單引號內含“ \\ t'”,但仍無法導出前導0
像這樣更改implode
使每個值都用單引號引起來:
echo "'".implode("'\t'", array_values($row)) . "'\r\n";
更新
僅引用前導零的字段:
echo implode("\t", array_map('leadZero', $row))."\r\n";
function leadZero($element)
{
return substr(trim($element),0,1) === "0" ? "'".$element."'" : $element;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.