Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel - set cell type before writing a value in it

Tags:

phpexcel

Do you know how can I set the cell type before writing a value in it? I would like to be able to set types like "General", "Text" and "Number".

Thank you.

like image 998
Psyche Avatar asked Jun 17 '13 08:06

Psyche


3 Answers

When the text is a number with leading zeros, then do: (Cuando el texto es un número que empieza por ceros, hacer)

$objPHPExcel->getActiveSheet()->setCellValueExplicit('A1', $val,PHPExcel_Cell_DataType::TYPE_STRING);
like image 194
FelipeR Avatar answered Oct 22 '22 23:10

FelipeR


The same way as you'd set the type (number format mask) after writing a value to it:

$objPHPExcel->getActiveSheet()
    ->getStyle('A1')
    ->getNumberFormat()
    ->setFormatCode(
        PHPExcel_Style_NumberFormat::FORMAT_GENERAL
    );

or

$objPHPExcel->getActiveSheet()
    ->getStyle('A1')
    ->getNumberFormat()
    ->setFormatCode(
        PHPExcel_Style_NumberFormat::FORMAT_TEXT
    );

Though "Number" isn't a valid format mask.

You can find a list of pre-defined format masks in Classes/PHPExcel/Style/NumberFormat.php or set the value to any valid Excel number format masking string.

like image 25
Mark Baker Avatar answered Oct 22 '22 22:10

Mark Baker


try this

$currencyFormat = '_($* #,##0.00_);_($* (#,##0.00);_($* "-"??_);_(@_)';
$textFormat='@';//'General','0.00','@'
$excel->getActiveSheet()->getStyle('B1')->getNumberFormat()->setFormatCode($currencyFormat);
$excel->getActiveSheet()->getStyle('C1')->getNumberFormat()->setFormatCode($textFormat);`
like image 4
Bladimir Ramirez Avatar answered Oct 23 '22 00:10

Bladimir Ramirez