强制fputcsv将输出括在引号中

时间:2013-12-12 14:45:37

标签: php magento csv fputcsv

我在网上发现了一些关于强制包含fputcsv输出的主题,并发现不可能这样做。由于一些导入要求,我需要用引号括起每个输出。单独的单词正在起作用,但非单独的单词不起作用。我看到有几个解决方法,但我不能让它们工作。例如,我有这段代码:

/**
 * Writes the row(s) for the given order in the csv file.
 * A row is added to the csv file for each ordered item. 
 * 
 * @param Mage_Sales_Model_Order $order The order to write csv of
 * @param $fp The file handle of the csv file
 */
protected function writeOrder($order, $fp) 
{
    $common = $this->getCommonOrderValues($order);
    $orderItems = $order->getItemsCollection();
    $itemInc = 0;
    foreach ($orderItems as $item)
    {
        if (!$item->isDummy()) {
            $record = array_merge($common, $this->getOrderItemValues($item, $order, ++$itemInc));
            fputcsv($fp, $record, self::DELIMITER, self::ENCLOSURE);
        }
    }
}

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

使用来自PEAR的https://pear.php.net/package/File_CSV/

的File_CSV包

您的代码看起来像这样,显然是不完整的:

<?php
require 'File/CSV.php'; 
$csv_conf = array(
    'fields' => 3, 
    'crlf' => "\r\n", 
    'quote' => '"', 
    'sep' => ','
);

$common = $this->getCommonOrderValues($order);
$orderItems = $order->getItemsCollection();
$itemInc = 0;
foreach($orderItems as $item) {
    if (!$item->isDummy()) {
        $record = array_merge(
            $common, 
            $this->getOrderItemValues($item, $order, ++$itemInc)
        );
        $res = File_CSV::write($fileName, $record, $csv_conf);
    }
}