I have an array that can store numbers in such as 01, 01A, 01B, 02, 2, and When I get this value using PHPExcel, it\'s being removed the \'0\'s in case of 01, 02, for exampl
You can format value to text with add charater before or after value. Example add after value charater ";"
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, $row,$user->msisdn.';' );
$objPHPExcel
->getActiveSheet()
->getCellByColumnAndRow($col, $row)
->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
I know this was all posted a while ago, but wanted to share something that worked for me, so you can still use ->fromArray
and not have to iterate over the whole spreadsheet.
If you wrap your values in ="VALUE"
it will come through as text, and not convert it, and it will not have quotes around it in your spreadsheet
Formatting using a number format affects the way a number is displayed, not the way it is stored.
You'll have to store the numbers explicitly as strings, so you can't use fromArray(). Use setCellValueExplicit() or setCellValueExplicitByColumnAndRow() instead, passing a $pDataType argument of PHPExcel_Cell_DataType::TYPE_STRING.
EDIT
Note that you can also set styles for a range of cells, so there's no need to add the overhead of the for loop:
$range = 'A'.$row.':'.$latestBLColumn.$row;
$objPHPExcel->getActiveSheet()
->getStyle($range)
->getNumberFormat()
->setFormatCode( PHPExcel_Style_NumberFormat::FORMAT_TEXT );
EDIT #2 Using a cell binder
Create a customised cell value binder:
class PHPExcel_Cell_MyValueBinder extends PHPExcel_Cell_DefaultValueBinder
implements PHPExcel_Cell_IValueBinder
{
public function bindValue(PHPExcel_Cell $cell, $value = null)
{
// sanitize UTF-8 strings
if (is_string($value)) {
$value = PHPExcel_Shared_String::SanitizeUTF8($value);
}
// Implement your own override logic
if (is_string($value) && $value[0] == '0') {
$cell->setValueExplicit($value, PHPExcel_Cell_DataType::TYPE_STRING);
return true;
}
// Not bound yet? Use default value parent...
return parent::bindValue($cell, $value);
}
}
To avoid any problems with the autoloader, create this in the /Classes/PHPExcel/Cell directory. Otherwise, give the class your own non-PHPExcel name, and ensure that it's loaded independently.
Then, before using your fromArray() call, tell PHPExcel to use your value binder instead of the default binder:
PHPExcel_Cell::setValueBinder( new PHPExcel_Cell_MyValueBinder() );
In case you need to convert the whole sheet numbers to text, you can use calculateWorksheetDimension()
to get the sheet's dimensions (example: 'A1:B200' or 'A1:C150') and then use it in getStyle()
, like so:
// Get sheet dimension
$sheet_dimension = $spreadsheet->getActiveSheet()->calculateWorksheetDimension();
// Apply text format to numbers
$spreadsheet->getActiveSheet()->getStyle($sheet_dimension)->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
Note: This example uses PhpSpreadsheet since it is the next version of PHPExcel.