user2393886
user2393886

Reputation: 872

Unable to set numeric value leading with zero as text using phpexcel

I have created an excel using PHPExcel library. Everthing going well but when i insert numeric value leading with zero then it removed the zero from that number.

                                foreach($option['option_value'] as $option_value){
                                $activeSheet->SetCellValue('A'.$row, $product['model']);
                                $activeSheet->SetCellValue('B'.$row, $product['name']);
                                $activeSheet->setCellValueExplicit('C'.$row, '0000', PHPExcel_Cell_DataType::TYPE_STRING);
                                if(empty($option_value['barcode'])){
                                    $activeSheet->SetCellValue('C'.$row, 'NA');
                                }else{
                                    $activeSheet->SetCellValue('C'.$row, $option_value['barcode']);                                        
                                }
                                $activeSheet->SetCellValue('D'.$row, $option_value['name']);
                                $activeSheet->SetCellValue('E'.$row, round($price,2));
                                $activeSheet->SetCellValue('F'.$row, $option_value['quantity']);
                                $row++;
                            }   

I have used following inbuilt function to set column type as text but nothing got:

$activeSheet->getStyle('C1:C'.$row)->getNumberFormat()->setFormatCode('00000');  or  
$activeSheet->getStyle('C1:C'.$row)->getNumberFormat()->setFormatCode('0');  or  
$activeSheet->getStyle('C')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);  

Can anyone tell me that what the thing i m doing wrong in above code? Is there any other alternative to fix the same.

Waiting for reply.

Upvotes: 0

Views: 3072

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

Numbers don't have leading zeroes. Either you have a string of digits, the first of which are zeroes; or you format a number to appear with leading zeroes.

Either:

// Set the value explicitly as a string
$objPHPExcel->getActiveSheet()
    ->setCellValueExplicit('C2', '0000', PHPExcel_Cell_DataType::TYPE_STRING);

or

// Set the value as a number formatted with leading zeroes
$objPHPExcel->getActiveSheet()
    ->setCellValue('C3', 0);
$objPHPExcel->getActiveSheet()
    ->getStyle('C3')
    ->getNumberFormat()->setFormatCode('0000');

Upvotes: 1

Related Questions