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
1 回答
aluckdog
TA贡献1847条经验 获得超7个赞
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;
}
- 1 回答
- 0 关注
- 159 浏览
添加回答
举报
0/150
提交
取消