PHP - 创建 CSV 文件会在末尾插入空行
PHP - Creating CSV file inserts empty row at the end
我正在尝试在我的 php 脚本中创建一个 CSV 文件。它会创建文件,但每次都会在末尾插入一个空行。
我的代码如下:
$csv_output .= "Header 0,";
$csv_output .= "Header 1,";
$csv_output .= "Header 2,";
$csv_output .= "Header 3,";
$csv_output .= "Header 4,";
$csv_output .= "\r\n";
// db query...
foreach($rows as $row):
if($row["value_3"] == 10)
{
$x = 'low';
}
else if($row["value_3"] == 100)
{
$x = 'high';
}
$csv_output .= "".$row["value_0"].",";
$csv_output .= "".$row["value_1"].",";
$csv_output .= "".$row["value_2"].",";
$csv_output .= "".$x.",";
$csv_output .= "Constant Always Value Goes here (not in db),";
$csv_output .= "\r\n";
endforeach;
$filename = "csv_".date("d-m-Y_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
无论我的数据库查询是什么或 csv 包含多少行,最后总是有一个空行。有什么原因造成的吗?
除了 Mark Baker 强调的其他问题外,您还在循环中添加了 EOL
。通过 trim
删除最后一个
print trim($csv_output);
您可以像这样直接将CSV写入输出,不需要自己格式化。
// Send correctly formatted headers
header("Content-type: application/vnd.ms-excel");
// Disposition should be "attachment" followed by optional filename.
header(sprintf('Content-disposition: attachment; filename="csv_%s.csv"', date("d-m-Y_H-i")));
// Open a pointer to the output stream
$fp = fopen('php://output', 'w');
// Build header array
$head = array(
"Header 0",
"Header 1",
"Header 2",
"Header 3"
);
// Write the header array in default csv row format.
fputcsv($fp, $head);
// db query...
// Loop rows
foreach($rows as $row) {
// Write the rows in default csv row format
fputcsv($fp, $row);
}
// Close the output stream
fclose($fp);
另一个好处是它会在大量下载时使用更少的内存,例如使用 PDO:
$stmt = $pdo->prepare("SELECT ...");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
fputcsv($fp, $row);
}
这将直接从数据库(或多或少)将每一行输出到浏览器,内存占用非常低。
我正在尝试在我的 php 脚本中创建一个 CSV 文件。它会创建文件,但每次都会在末尾插入一个空行。
我的代码如下:
$csv_output .= "Header 0,";
$csv_output .= "Header 1,";
$csv_output .= "Header 2,";
$csv_output .= "Header 3,";
$csv_output .= "Header 4,";
$csv_output .= "\r\n";
// db query...
foreach($rows as $row):
if($row["value_3"] == 10)
{
$x = 'low';
}
else if($row["value_3"] == 100)
{
$x = 'high';
}
$csv_output .= "".$row["value_0"].",";
$csv_output .= "".$row["value_1"].",";
$csv_output .= "".$row["value_2"].",";
$csv_output .= "".$x.",";
$csv_output .= "Constant Always Value Goes here (not in db),";
$csv_output .= "\r\n";
endforeach;
$filename = "csv_".date("d-m-Y_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
无论我的数据库查询是什么或 csv 包含多少行,最后总是有一个空行。有什么原因造成的吗?
除了 Mark Baker 强调的其他问题外,您还在循环中添加了 EOL
。通过 trim
print trim($csv_output);
您可以像这样直接将CSV写入输出,不需要自己格式化。
// Send correctly formatted headers
header("Content-type: application/vnd.ms-excel");
// Disposition should be "attachment" followed by optional filename.
header(sprintf('Content-disposition: attachment; filename="csv_%s.csv"', date("d-m-Y_H-i")));
// Open a pointer to the output stream
$fp = fopen('php://output', 'w');
// Build header array
$head = array(
"Header 0",
"Header 1",
"Header 2",
"Header 3"
);
// Write the header array in default csv row format.
fputcsv($fp, $head);
// db query...
// Loop rows
foreach($rows as $row) {
// Write the rows in default csv row format
fputcsv($fp, $row);
}
// Close the output stream
fclose($fp);
另一个好处是它会在大量下载时使用更少的内存,例如使用 PDO:
$stmt = $pdo->prepare("SELECT ...");
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
fputcsv($fp, $row);
}
这将直接从数据库(或多或少)将每一行输出到浏览器,内存占用非常低。