symfony将html.twig导出到csv

 琪琪格 发布于 2023-02-09 13:37

我正在尝试将树枝视图输出到csv中,但是我被卡住了,有人可以用详细的解决方案帮助我吗?和平

/**
 *  
 * @Route("/export", name="export_csv")
 */
public function exportAction() {

    $entity = new Invite();
    $form = $this->createForm(new ManifSearchType(), $entity);
    $request = $this->get('request');

    $em = $this->getDoctrine()->getManager();

    $view = $this->render('PrifProtocoleBundle:Invite:index.html.twig', array(
        'form' => $form->createView()));

    $handle = fopen('php://memory', 'r+');
    $header = array();

    fputcsv($handle, $view);

    rewind($handle);

    $content = stream_get_contents($handle);
    fclose($handle);

    return new Response($content, 200, array(
        'Content-Type' => 'application/force-download',
        'Content-Disposition' => 'attachment; filename="export.csv"'
    ));
} 

错误信息:

Warning: fputcsv() expects parameter 2 to be array, object given in C:\wamp\www\protocole\src\Prif\ProtocoleBundle\Controller\InviteController.php line 56 

Jean-Françoi.. 5

问题是,您不是只想将树枝转换为CSV文件.您正在将HTML转换为CSV文件.现在,这似乎是一个奇怪的转换.

你应该做的是让你的树枝生成CSV内容.像这样:

$response = $this->render('PrifProtocoleBundle:Invite:export.csv.twig',array('data' => $data));
$response->headers->set('Content-Type', 'text/csv');
$response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
return $response;

你的树枝应该以CSV格式呈现.

1 个回答
  • 问题是,您不是只想将树枝转换为CSV文件.您正在将HTML转换为CSV文件.现在,这似乎是一个奇怪的转换.

    你应该做的是让你的树枝生成CSV内容.像这样:

    $response = $this->render('PrifProtocoleBundle:Invite:export.csv.twig',array('data' => $data));
    $response->headers->set('Content-Type', 'text/csv');
    $response->headers->set('Content-Disposition', 'attachment; filename="export.csv"');
    return $response;
    

    你的树枝应该以CSV格式呈现.

    2023-02-09 13:43 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有