PHPOFFICESPREEDSHEET CELL FORMAT

68 Views Asked by At

I am using PhpOffice\PhpSpreadsheet\Spreadsheet to generate a .csv file. I have a number in a cell but I want the number to be formatted as text so no formulas apply to the cell. The problem is that the number eg 58 I should put as '58 and excel should automatically convert that to text. (That cell should have a green triangle at the top left and the ' before the number should not be visible in that cell-Can be seen in the Formula bar.) However the cell shows the number as I put it('58) and this makes the file invalid. My current code is

$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
.......
$sheet->setCellValue('A2', "'58");
......
$sheet->getStyle('A2:K' . $sheet->getHighestRow())
      ->getAlignment()
      ->setHorizontal(PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);
$sheet->getStyle('A2:F' . $sheet->getHighestRow())
      ->getNumberFormat()
      ->setFormatCode(PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
.....
$writer = new PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('file_name.xlsx');

What might I be doing wrong?

What my code produces:

image 1

What I expect:

image 2

0

There are 0 best solutions below