PHP Symfony 4无法导出CSV文件中的数据

时间:2018-12-13 00:04:57

标签: php export-to-csv symfony4

我的以下代码未返回任何错误,但是,它返回的是HTML页面中的数据字符串,而不是返回CSV文件。我不知道我在symfony设置中错过了什么。

    $em = $this->getDoctrine()->getManager();
    $sql = "select name, id from table";
    $stmt = $em->getConnection()->getWrappedConnection()->prepare($sql);
    $stmt->execute();

    $source = new PDOStatementSourceIterator($stmt);

    $format='csv';
    $export_to='php://output';
    $exporterWriter='\Exporter\Writer\\'.ucfirst($format).'Writer';
    $writer = new $exporterWriter($export_to);

    Handler::create($source, $writer)->export();

    $response = new Response();
    $response->headers->set('Content-Type', 'text/csv');
    $response->headers->set('Content-Disposition', 'attachment; filename="data.csv"');

    return $response;

0 个答案:

没有答案