我需要幫助。現在我正在創建直接下載 CSV 檔案,當從前端按下匯出按鈕時。但我總是得到一個空的 csv 檔案。在代碼中,我還將 CSV 檔案放在特定檔案夾中。它可以顯示正確的 CSV 格式
這是我的代碼
//CSV output
$fileName = 'CSV-Export.csv';
$rows = [];
foreach ($lists as $key => $value) {
$rows[$key]['product_id'] = $value['product_id'];
$rows[$key]['product_name'] = $value['product_name'];
$rows[$key]['price'] = $value['price'];
}
$columnNames = [
'Product ID',
'Product Name',
'Price'
];
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=".$fileName);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Content-Transfer-Encoding: UTF-8");
$file = fopen("csv/".$fileName, "w");
fwrite($file, "Report Export"."\r\n");
fwrite($file, " ". "\r\n");
fputcsv($file, $columnNames);
foreach ($rows as $row) {
fputcsv($file, [
$row['product_id'],
$row['product_name'],
$row['price']
]);
}
fclose($file);
我的代碼有什么問題。請幫忙
uj5u.com熱心網友回復:
更新。讓它變得更簡單)
首先要做的事)您在發送標頭后創建檔案,這就是您收到空 csv 檔案的原因。
$lists您可以在下面找到帶有填充陣列的簡化代碼示例。
<?php
$fileName = 'CSV-Export.csv';
$lists = [
[
'product_id' => 1,
'product_name' => "product_1",
'price' => 150
],
[
'product_id' => 2,
'product_name' => "product_2",
'price' => 160
]
];
$columnNames = [
'Product ID',
'Product Name',
'Price'
];
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=".$fileName);
header('Cache-Control: must-revalidate');
header("Content-Transfer-Encoding: UTF-8");
echo "Report Export \r\n";
echo " \r\n";
echo implode(",", $columnNames) . "\r\n";
foreach ($lists as $key => $value) {
echo $value['product_id'] . "," . $value['product_name'] . "," . $value['price'] . "\r\n";
}
exit(0);
?>
另一個解決方案更接近你的)
<?php
//CSV output
$fileName = 'CSV-Export.csv';
$lists = [
[
'product_id' => 1,
'product_name' => "product_1",
'price' => 150
],
[
'product_id' => 2,
'product_name' => "product_2",
'price' => 160
]
];
$columnNames = [
'Product ID',
'Product Name',
'Price'
];
$file = fopen("csv/" . $fileName, "w");
fwrite($file, "Report Export"."\r\n");
fwrite($file, " ". "\r\n");
fputcsv($file, $columnNames);
foreach ($lists as $key => $value) {
fputcsv($file, [
$value['product_id'], $value['product_name'], $value['price']
]);
}
fclose($file);
$stream = fopen("csv/" . $fileName, "r");
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=".$fileName);
header('Cache-Control: must-revalidate');
header("Content-Transfer-Encoding: UTF-8");
echo fread($stream, filesize("csv/" . $fileName));
exit(0);
?>
uj5u.com熱心網友回復:
您需要檢查fputcsv 檔案欄位是一個字串陣列,您可以更改代碼:
$rows = [];
foreach ($lists as $key => $value) {
$rows[$key]['product_id'] = $value['product_id'];
$rows[$key]['product_name'] = $value['product_name'];
$rows[$key]['price'] = $value['price'];
}
到:
$rows = array();
foreach ($lists as $key => $value) {
$temp=array();
$temp[]=$value['product_id'];
$temp[]=$value['product_name'];
$temp[]=$value['price'];
$rows[]=$temp;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376581.html
上一篇:坐標輸出到csv檔案python
