Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Excel return false to string with special chars

Tags:

php

phpexcel

I have several strings from a form ($_POST var), some of them with special chars (ñ, á, é, etc.). When I create the Excel file with these $_POST vars, all cells print the right information but the ones with special chars prints FALSE.

Here's a piece of code:

for ($j=1; $j<$_POST["z"]; $j++) {
$fila_despacho = $j+1;  
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$fila_despacho, '03') 
->setCellValue('B'.$fila_despacho, $_POST["servicio".$j])
->setCellValue('C'.$fila_despacho, trim ($_POST["comuna".$j]))
->setCellValue('D'.$fila_despacho, 1)
->setCellValue('E'.$fila_despacho, trim($_POST["nombre".$j]))
->setCellValue('F'.$fila_despacho, trim($_POST["direccion".$j]))
        ->setCellValue('G'.$fila_despacho, trim($_POST["num".$j]));    
}//endfor

About UTF-8, I don't know how to check that, I'm new at this. Can you tell me how?

like image 323
Pamela Valenzuela Avatar asked May 01 '26 01:05

Pamela Valenzuela


1 Answers

Change to UTF-8 before setting the cell value. Use utf8_encode(). Example:

$objPHPExcel->getActiveSheet()->setCellValue('A1', utf8_encode($descripcion));

Later on you can save the file, for example with:

header('Content-Type: application/vnd.ms-excel;  charset=UTF-8');  
header("Content-Disposition: attachment;filename='$archivo'");
like image 178
Andrew Avatar answered May 02 '26 15:05

Andrew