c.wen
c.wen

Reputation: 175

number cell format with text

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)

Upvotes: 11

Views: 25633

Answers (3)

Alejandro De Castro
Alejandro De Castro

Reputation: 2247

This works, in all sheet.

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

Upvotes: 5

Agung Kessawa
Agung Kessawa

Reputation: 563

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);

Upvotes: 19

Collie-IT Anne K. Frey
Collie-IT Anne K. Frey

Reputation: 875

You can set the formatcode to @ (strings)

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

Upvotes: 6

Related Questions