1 [This is the output] Trying to export SQL data as excel file by looping the array but failed to export the leading 0 of data.
I had tried to place single quote in the implode but still not working
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";
}
Expect to implode with "\\t'" with the single quote but still failed to export the leading 0
Change your implode
like this so that each value is quoted in single quotes:
echo "'".implode("'\t'", array_values($row)) . "'\r\n";
UPDATE
Quoting only fields with a leading zero:
echo implode("\t", array_map('leadZero', $row))."\r\n";
function leadZero($element)
{
return substr(trim($element),0,1) === "0" ? "'".$element."'" : $element;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.