11

I want to export my data to excel using phpspreadsheet but my data is number in 12 character. So I need to display all the character (121212121212) instead of (1.21212E+11).

I have try the format using

PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT

and

PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER

It doesn't work.

This is my cell formatting code:

$spreadsheet->getActiveSheet()->getStyle('B')->getNumberFormat()
    ->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);

This is my value insert code:

$sheet->setCellValue('A'.$i, $i-1);
        $sheet->setCellValue('B'.$i, $useric);

If I use the FORMAT_TEXT the result is this:

![result](https://i.imgur.com/6xxKDkv.png)

3 Answers 3

19

when using PHPSpreadsheet, this is what I do.

$spreadsheet->getActiveSheet()
            ->getCell('A1')
            ->setValueExplicit(
                $someNumber,
                \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING2
            );

If by any chance you using PHPExcel, maybe you can do something like this

$spreadsheet->getActiveSheet()
            ->setCellValueExplicit('A1', $someNumber, PHPExcel_Cell_DataType::TYPE_STRING);
1
  • Really surprised how difficult it was for me to find your answer on this one, with the naming conventions between the 2 and how bad google has gotten when searching for help on this stuff, it's like good luck dealing with debugging between the 2.
    – Jestep
    Feb 22 at 19:52
6

You can set the formatcode to @ (strings)

$spreadsheet->getActiveSheet()->getStyle('B')->getNumberFormat()
    ->setFormatCode('@');
4

This works, in all the sheet

$spreadsheet ->getDefaultStyle()->getNumberFormat()->setFormatCode('#');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.