project.'",'; -6ren">
gpt4 book ai didi

PHP 导出到 CSV 类

转载 作者:行者123 更新时间:2023-12-04 18:04:14 27 4
gpt4 key购买 nike

我编写了以下类来将数据库结果导出到 CSV 文件。

<?php
class Export {
public static function tocsv($results = array(), $fields = array()) {
$schema_insert = '"'.implode('","', $fields).'"';
$out .= $schema_insert."\n";

foreach($results as $row) {
$schema_insert = '';
$schema_insert .= '"'.$row->week_ending.'",';
$schema_insert .= '"'.$row->project.'",';
$schema_insert .= '"'.$row->employee.'",';
$schema_insert .= '"'.$row->plots.'",';
$schema_insert .= '"'.$row->categories.'"';
$out .= $schema_insert."\n";
}
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out));
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out;
exit;
}
}
?>

输出是:

"Week ending","Project name","Plot numbers","Categories","Employee"
"Friday 08 May 2015","Big Road","Tracey Smith","1A, 2A, 3A"," Water meter, 1st fix inc lagging"

但是,当我用 Excel 打开时,所有内容都在一列中。我错过了什么吗?

谢谢。

最佳答案

使用分号作为字段终止符而不是逗号,这确实是 Excel 的东西,而不是 php 的东西。

关于PHP 导出到 CSV 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29944005/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com