0

我正在使用 cakephp 2.3,并且需要在成功下载 excel 表后重定向用户。我正在使用 cake $this->response->type 将视图设置为 excel 表生成器。

public function admin_export_excel($task_lists = array()) {
    $task_ids = $task_lists;
    $global_task_array = array();
    $this->loadModel('Task');
    //-> For each task-id run the loop for fetching the related data to generate the report.
    foreach ($task_ids as $index => $task_id) {
        //-> Check if the task exists with the specified id.
        $this->Task->id = $task_id;
        if (!$this->Task->exists())
            throw new NotFoundException('Task not found.');

        //-> Now check if the logged user is the owner of the specified task. 
        $task_count = $this->Task->find('count', array('conditions' => array('Task.id' => $task_id,
                'Task.user_id' => $this->Auth->user('id'))));
        if ($task_count == 0)
            throw new NotFoundException('Task not accessable.');

        $task_data = $this->Task->find('first', array(
            'conditions' => array(
                'Task.id' => $task_id
            ),
            'contain' => array(
                'TaskForm' => array(
                    'fields' => array('TaskForm.id', 'TaskForm.reference_table')
                ),
                'Project' => array(
                    'fields' => array('Project.id', 'Project.project_name')
                ),
                'User' => array(
                    'fields' => array('User.id', 'User.company_name')
                ),
                'Timezone' => array(
                    'fields' => array('Timezone.id', 'Timezone.name')
                )
            )
                )
        );
        // debug($task_data);

        $global_task_array[$index] = $task_data;

        //-> End of Custom else conditions                    
        unset($task_data);
    }

    $this->set('global_task_array', $global_task_array);
    $this->response->type(array('xls' => 'application/vnd.ms-excel'));
    $this->response->type('xls');
    $this->render('admin_export_excel');
}

我的视图文件是

    $this->PhpExcel->createWorksheet();
$this->PhpExcel->setDefaultFont('Calibri', 13);

$default = array(
    array('label' => __('Task Id'), 'width' => 'auto'),
    array('label' => __('Unique Code'), 'width' => 'auto'),
    array('label' => __('Site Name'), 'width' => 'auto'),
    array('label' => __('Area'), 'width' => 'auto'),
    array('label' => __('Location'), 'width' => 'auto'),
    array('label' => __('Sub Location'), 'width' => 'auto'),
    array('label' => __('About Task'), 'width' => 'auto')
  );

$this->PhpExcel->addTableHeader($default, array('name' => 'Cambria', 'bold' => true));
$this->PhpExcel->setDefaultFont('Calibri', 12);
   foreach ($global_task_array as $index => $raw) {
            $data = array(
            $raw['Task']['id'],
            $raw['Task']['unique_code'],
            $raw['Task']['site_name'],
            $raw['Task']['area'],
            $raw['Task']['location'],
            $raw['Task']['sub_location'],
            $raw['Task']['about_task']
            );

          $this->PhpExcel->addTableRow($data);
       }  
   $this->PhpExcel->addTableFooter();
   $this->PhpExcel->output('Task-' . date('d-m-Y') . '.xlsx');

我尝试使用 cake afterFilter 方法在生成 excel 表后将用户重定向到其他操作,但它不起作用。

public function afterFilter(){
   parent::afterFilter();

    if($this->request->params['action'] == 'admin_export_excel'){           
        $this->redirect(array('controller' => 'tasks', 'action' => 'index','admin' => true));
    }
}

任何帮助将不胜感激。谢谢

4

0 回答 0