尝试使用 excel mime 导出具有前导 0 的特定 SQL 数据

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";

}

Expect to implode with "\t'" with the single quote but still failed to export the leading 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;
}