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.
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);
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);`
For Numbers with leading zeroes and comma separated:
You can put 'A' to affect the entire column'.
$objPHPExcel->getActiveSheet()->getStyle('A1')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_COMMA_SEPARATED1);
Then you can write to the cell as you normally would.
I wanted the Number same as I get from database for example.
1) 00100.220000
2) 00123
3) 0000.0000100
So I modified the code as below
$objPHPExcel->getActiveSheet()
->setCellValue('A3', '00100.220000');
$objPHPExcel->getActiveSheet()
->getStyle('A3')
->getNumberFormat()
->setFormatCode('00000.000000');
$objPHPExcel->getActiveSheet()
->setCellValue('A4', '00123');
$objPHPExcel->getActiveSheet()
->getStyle('A4')
->getNumberFormat()
->setFormatCode('00000');
$objPHPExcel->getActiveSheet()
->setCellValue('A5', '0000.0000100');
$objPHPExcel->getActiveSheet()
->getStyle('A5')
->getNumberFormat()
->setFormatCode('0000.0000000');
Followed Mark's advise and did this to set the default number formatting to text in the whole workbook:
$objPHPExcel = new PHPExcel();
$objPHPExcel->getDefaultStyle()
->getNumberFormat()
->setFormatCode(
PHPExcel_Style_NumberFormat::FORMAT_TEXT
);
And it works flawlessly. Thank you, Mark Baker.
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.