php headers excel number format

0

I need help printing numbers with a decimal when it comes to thousands when generating an excel with php. What is happening is I want to show only a decimal when printing the sheet, but it happens that having numbers of thousands, for example, 1,298.8 shows me with two decimals and 1,298.80. It only happens with the thousands. The data in the database is fine. The headers that I have are the following:

date_default_timezone_set('America/Santiago');
header('Content-disposition: attachment; filename=ficheroExcel.xls');
header("Content-Type: text/plain; charset=UTF-8");
header('Content-Transfer-Encoding: 8bit');
header('Cache-Control: must-revalidate');
header('Pragma: public');
    
asked by user2820116 11.12.2018 в 14:57
source

1 answer

1

You have to define it in the styles of the cell.

For each cell, or range of cells, you can say the type of data and / or format.

/* Ejemplo de formato numérico, sin decimales */
$objPHPExcel
   ->getActiveSheet()
   ->getStyle("A1")
   ->getNumberFormat()
   ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER); 

/* Ejemplo de formato numérico personalizado */
$objPHPExcel->getActiveSheet()->getStyle("A1")->applyFromArray(
    "numberFormat" => array(
       "code" =>  "#,###.#"  /* formato personalizado*/
    )
)

In PHPExcel's NumberFormat class you have several predefined formats, in addition to being able to give you the custom format you want.

    
answered by 11.12.2018 в 16:35