CSV导出显示HTML和数据。为什么?

时间:2014-01-15 00:08:06

标签: php symfony export-to-csv

这是我的代码,我正在使用https://github.com/sonata-project/exporter

$data = array(
                    0 => array(
                        'name' => 'Jack'
                    ),
                    1 => array(
                        'name' => 'Jill'
                    )
                );

    $format = 'csv';

                    // Filename
                    $filename = 'referral.csv';

                    // Set Content-Type
                    $content_type = 'text/csv';

                    // Location to Export this to
                    $export_to = 'php://output';

                    // Data to export
                    $exporter_source = new \Exporter\Source\ArraySourceIterator($data);

                    // Get an Instance of the Writer
                    $exporter_writer = '\Exporter\Writer\\' . ucfirst($format) . 'Writer';

                    $exporter_writer = new $exporter_writer($export_to);

                    // Set the right headers
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Content-Description: File Transfer');
                    header('Content-type: ' . $content_type);
                    header('Content-Disposition: attachment; filename=' . $filename . ';');
                    header('Expires: 0');
                    header('Pragma: public');

                    // Export to the format
                    \Exporter\Handler::create($exporter_source, $exporter_writer)->export();

当我执行以下代码时,它会下载一个包含数组数据BUT的CSV文件,该文件也包含该页面的HTML。如何使导出只包含数据数组的元素?

1 个答案:

答案 0 :(得分:2)

此代码来自SonataCoreBundle,它将响应流式传输到客户端,因此它有利于内存消耗:

use Exporter\Source\SourceIteratorInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;

use Exporter\Writer\XlsWriter;
use Exporter\Writer\XmlWriter;
use Exporter\Writer\JsonWriter;
use Exporter\Writer\CsvWriter;

// ...

public function exportAction()
{
    $data = array(array('name' => 'Jack'), array('name' => 'Jill'));
    $source = new \Exporter\Source\ArraySourceIterator($data);
    $filename = 'file.csv'
    $format = 'csv';

    switch ($format) {
        case 'xls':
            $writer      = new XlsWriter('php://output');
            $contentType = 'application/vnd.ms-excel';
            break;
        case 'xml':
            $writer      = new XmlWriter('php://output');
            $contentType = 'text/xml';
            break;
        case 'json':
            $writer      = new JsonWriter('php://output');
            $contentType = 'application/json';
            break;
        case 'csv':
            $writer      = new CsvWriter('php://output', ',', '"', "", true, true);
            $contentType = 'text/csv';
            break;
        default:
            throw new \RuntimeException('Invalid format');
    }

    $callback = function() use ($source, $writer) {
        $handler = \Exporter\Handler::create($source, $writer);
        $handler->export();
    };

    return new StreamedResponse($callback, 200, array(
        'Content-Type'        => $contentType,
        'Content-Disposition' => sprintf('attachment; filename=%s', $filename)
    ));
}