我想为我的每一列添加一个标题,这是我的代码:
$products = $order->getProducts();
// Création CSV détail de commande
$filename = $order->reference . '_details.csv';
if (!$fp = fopen($this->exportDir . '/' . $filename, "w")) {
throw new Exception('Impossible de créer fichier export : ' . $this->exportDir . '/' . $filename);
}
$header = ['CMD', 'Reference', 'Quantity', 'Unit Price', 'Total Price'];
fputcsv($fp, $header);
foreach ($products as $product) {
$data = array();
$data[] = $order->reference;
$data[] = (string)$product['product_reference'];
$data[] = $product['product_quantity'];
$data[] = number_format($product['unit_price_tax_incl'], 2);
$data[] = number_format($product['total_price_tax_incl'], 2);
fputcsv($fp, $data, $this->csvDelimiter, $this->csvEnclosure);
}
fclose($fp);
我尝试不同的方法,但是标题将其放在一个单元格上..谢谢您的帮助。
答案 0 :(得分:1)
您需要指定标题定界符和内容,以使其与您的数据匹配:
更改:
$header = ['CMD', 'Reference', 'Quantity', 'Unit Price', 'Total Price'];
fputcsv($fp, $header);
进入
$header = ['CMD', 'Reference', 'Quantity', 'Unit Price', 'Total Price'];
fputcsv($fp, $header, $this->csvDelimiter, $this->csvEnclosure);