PHPExcel error with Codeigniter

1

I am using PHPExcel with Codeigniter in a fairly simple way:

$this->load->library('PHPExcel/Classes/PHPExcel');              
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Gastos")->setDescription("Descripcion de gastos");
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'NOMBRE GASTO');
$objPHPExcel->getActiveSheet()->setCellValue('B1', 'value_1');

$i = 1;

foreach($q->result() as $row){             
     $i++;                    
     $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, utf8_encode($row->name));
     $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $row->value_1);
}

Where the variable $q is a query, I'm sure that $row->name and $row->value_1 correctly bring the data, to finish I have this other code:

header('Content-Type: application/vnd.ms-excel');                                
header('Content-Disposition: attachment;filename="GASTOS.xls"');       
header('Cache-Control: max-age=0');     
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

My problem is that when downloading the excel it shows me this warning and the document in the following way:

I can not find the error nor can I correct this even if I try ...

    
asked by Jorius 04.11.2016 в 20:39
source

2 answers

0

At the end I was able to solve my problem by changing this in my code:

// Código anterior
header('Content-Type: application/vnd.ms-excel');                                
header('Content-Disposition: attachment;filename="GASTOS.xls"');       
header('Cache-Control: max-age=0');     
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

// Código nuevo que me solucionó el problema
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$uniqid = uniqid();
$objWriter->save('./assets/files/Gastos_' . $uniqid . '.xls');
redirect(base_url('/assets/files/Gastos_' . $uniqid . '.xls'));
    
answered by 06.11.2016 / 15:55
source
0

Taking into account your code and making a couple of modifications would be like this:

$this->load->library('PHPExcel/Classes/PHPExcel');              
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("Gastos")->setDescription("Descripcion de gastos");

//Encabezado del excel
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', 'NOMBRE GASTO');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B1', 'value_1');

//Lo inicializo en dos ya que en la primera fila se va a escribir el encabezado 
$i = 2;

foreach($q->result() as $row){             
     $i++;                    
     //Decode en lugar de encode ya que lo que hace decode en decodificar el texto
     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i, utf8_decode($row->name));
     $objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$i, $row->value_1);
}

$objPHPExcel->setActiveSheetIndex(0);

To save I occupy a different type of sentence but the same thing appears to me about the format of the file but it opens it correctly.

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="GASTOS.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
    
answered by 04.11.2016 в 21:31