Error creating and downloading XLS with Cakephp

Asked

Viewed 92 times

2

I have the following code:

    $this->autoRender = false;
    $this->autoLayout = false;
    $turmaId = base64_decode($turmaId);
    $turma = $this->Turma->find('first',array(
        'conditions'=>array(
            'Turma.id'=>$turmaId
        ),
        'fields'=>array(
            'Turma.turma','Serie.serie'
        ),
        'recursive'=>0
    ));
    echo 'Alunos do ' . iconv("UTF-8", "UTF-16LE//IGNORE",$turma['Serie']['serie']) . ' - Turma ' . iconv("UTF-8", "UTF-16LE//IGNORE",$turma['Turma']['turma'])."\r\n";
    echo 'Nome'."\t".'Email'."\t".iconv("UTF-8", "UTF-16LE//IGNORE",'Aniversário')."\t".'Data Cadastro'."\r\n";
    $data = $this->Turma->query('SELECT
    CONCAT(Pessoa.primeiro_nome," ",Pessoa.ultimo_nome) AS Nome,
    Pessoa.email,
    DATE_FORMAT(Aluno.created,"%d/%m/%Y") AS DataCadastro,      
    DATE_FORMAT(Pessoa.data_nascimento,"%d/%m/%Y") AS Aniversario       
    FROM alunos AS Aluno
    LEFT JOIN pessoas AS Pessoa
    ON Aluno.pessoa_id = Pessoa.id
    WHERE Aluno.turma_id = '.$turmaId.'
    ORDER BY Nome');
    foreach($data as $data) {
        echo iconv("UTF-8", "UTF-16LE//IGNORE",$data[0]['Nome']);
        echo "\t".$data['Pessoa']['email'];            
        echo "\t".$data[0]['Aniversario'];
        echo "\t".$data[0]['DataCadastro'];
        echo "\r\n";
    }

    $nomeArquivo = Inflector::slug('Alunos '.$turma['Serie']['serie'].' '.$turma['Turma']['turma']);
    $this->response->disableCache();
    $this->response->type('xls'); 
    $this->response->download('teste.xls');
    $this->response->body();       
    $this->response->send(); 

Local is working properly ie, create and download the XLS with the data I need. But when climbing to the server, it does not generate the file and the data is printed on the screen. Any solution?

Edit--

I solved the problem using the Phpexcel Component, available at: https://github.com/segy/PhpExcel

  • What is the error generated?

  • It doesn’t actually generate an error. What happened is that the data was being printed on the screen instead of being written to the file.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.