Export to excel from mysql with php and keep zeros to the left

0

I have the result of a query mysql that contains numbers with leading zeros, which I must keep on exporting to Excel. example: in BD the number 000018 appears when exporting to excel in the cell appears 18

This is the function I use to export.

<?php
require_once('conexion_db.php');
$slq = "select * from " . TABLE_EXPORT_PATRIMONIO . " ep, " . TABLE_EXPORT_COMPARATIVA_PATRI . " ex where ex.clave_patri = ep.entrega01_clave and ep.entrega01_envases <> '' and ep.entrega01_envases <> '0' and ep.entrega01_indicaciones = '' and ex.codigo_op <> ''";
$stmt = mysqli_query($conn, $slq);
$data = [];
$i = 1;
while($row = mysqli_fetch_assoc($stmt)){
    $data[$i]['id'] = $i;
    $data[$i]['codigo_op'] = $row['codigo_op'];
    $data[$i]['entrega01_clave'] = $row['entrega01_clave'];
    $data[$i]['descripcion_articulo'] = $row['descripcion_articulo'];
	$data[$i]['entrega01_envases'] = $row['entrega01_envases'];
	$data[$i]['entrega01_precio_unidad'] = $row['entrega01_precio_unidad'];
    $i++;
}
require_once('export_patrimonio_mysql_excel.php');
$export = new Export();

if(isset($_GET['export']) && $_GET['export'] == 'excel'){
    $export->excel('', $_GET['fileName'], $data);
}
?>

export_patrimonio_mysql_excel.php

<meta charset="UTF-8">
<?php
class Export{
    public function excel($name, $fileName, $data){
        // nome do arquivo
        $fileName = $fileName . '.xls';
        // Abrindo tag tabela e criando título da tabela
        $html = '';
        $html .= '<table border="0">';
        $html .= '<tr>';
		$html .= '<tr>';
		$html .= '<tr>';
		$html .= '<tr>';
		$html .= '<tr>';
		$html .= '<tr>';
		$html .= '<tr>';
        //$html .= '<th colspan="' . count($data) . '">' . $name . '</th>';
        $html .= '</tr>';
        $html .= '<tr>';
        // AQUI inicio COLOCAS EL TITULO DEL EXCEL SI LO NECESITAS//
		
		//foreach ($data[0] as $k => $v){
         //   $html .= '<th>' . ucfirst($k) . '</th>';
       // }
	   
	   // AQUI fin COLOCAS EL TITULO DEL EXCEL SI LO NECESITAS//
        $html .= '</tr>';
        // criando o conteúdo da tabela
        for($i=1; $i <= count($data); $i++){
            $html .= '<tr>';
            foreach ($data[$i] as $k => $v){
                $html .= '<td>' . $v . '</td>';
            }
            $html .= '</tr>';
        }
        $html .= '</table>';

        // configurando header para download
        header("Content-Description: PHP Generated Data");
        header("Content-Type: application/x-msexcel");
        header("Content-Disposition: attachment; filename=\"{$fileName}\"");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        // envio conteúdo
        echo $html;
        exit;
    }

    public function xml($data){

    }
}

The field codigo_op contains numbers with leading zeros and I need to keep them when I export the excel the zeros appear to the left, as I indicated in the example. 000018

    
asked by Ivan Diaz Perez 31.08.2017 в 13:59
source

2 answers

0

Add an apostrophe in the values so that excel recognizes the zeros

 foreach ($data[$i] as $k => $v){
     //cambia a comillas y poner apostrofe delante del valor
     $html .= "<td>'" . $v . "</td>";
 }
    
answered by 31.08.2017 в 16:20
0

try putting this:

$style='mso-number-format:"@";'

foreach ($data[$i] as $k => $v){
    $html .= "<td style='".$style."'>" . $v . "</td>";
}
    
answered by 06.12.2017 в 18:39