无法使用CsvView插件将数据保存到csv文件

时间:2019-03-24 18:20:12

标签: php csv cakephp export-to-csv

我要将来自db的请求结果保存到csv文件中。但只希望我拥有的是csv文件中的html代码...

那是我的current-project.ctp

<section class="content-header">
  <h1>

    <?php
    foreach($currentProject as $i){
        // debug($i);
        echo __('Details of the project: ').$i['shortname'];
    }
     ?>
    <hr>
  </h1>
  <ol class="breadcrumb">
    <li>
    <?= $this->Html->link('save', [
        'controller' => 'projects', 
        'action' => 'export',
        // '_ext' => 'csv'
        ],

        ['class' => 'btn btn-success']) 
      ?>
    </li>
    <li>
    <?= $this->Html->link('<i class="fa fa-dashboard"></i> '.__('Back'), ['action' => 'index'], ['escape' => false]) ?>
    </li>
  </ol>
</section>

这是我在控制器中的导出功能:ProjectsController.php

public function export() {
        $this->response->download('export.csv');
        $data = [
            ['a', 'b', 'c'],
            [1, 2, 3],
            ['you', 'and', 'me'],
        ];
        $_serialize = 'data';
        // $this->response->download('export.csv');

        $this->viewBuilder()->setClassName('CsvView.Csv');
        $this->set(compact('data', '_serialize'));
        return;
    }

路线:Router :: extensions(['csv']);

结果就是...我想在文件$ data数组中的单元格中看到

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>ContentSYSTEM</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width
    <!-- Bootstrap 3.3.5 -->
    <link rel="stylesheet" href="/admin_l_t_e/bootstrap/css/bootstrap.min.css"/>    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css">
    <!-- Ionicons -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css">
    <!-- Theme style -->
    <link rel="stylesheet" href="/admin_l_t_e/css/AdminLTE.min.css"/><!-- AdminLTE Skins. Choose a skin from the css/skins
...

1 个答案:

答案 0 :(得分:-1)

您应该在下面将其添加到您的控制器中:

use Cake\View\ViewBuilder;