2

I copied this code from PhpSpreadsheet's documentation and it does not work. This example can be found on this page:
LINK to PhpSpreadsheet's documentation

From the above link


enter image description here


My code:

$sheet->getCell('A2')->setValue(19);
$sheet->getStyle('A2')->getNumberFormat()->setFormatCode('0000'); 
// will show as 0019 in Excel

But its not working and this is the result:

enter image description here

The expected value of cell $A2 should be 0019 but it shows 19.

Any idea on how to fix this?

Misunderstood
  • 5,534
  • 1
  • 18
  • 25
Dmathz
  • 47
  • 7
  • Does it need tol be a numeric value or is a string okay? Con you create a CSV rather than a spreadsheet? – Misunderstood Oct 12 '22 at 03:59
  • Does this answer your question? [How to get phpexcel to keep leading 0s in phone numbers?](https://stackoverflow.com/questions/9635178/how-to-get-phpexcel-to-keep-leading-0s-in-phone-numbers) – Punit Gajjar Oct 12 '22 at 04:55
  • Thank you for your comment @PunitGajjar but it's still not working :( – Dmathz Oct 12 '22 at 05:44

1 Answers1

-1

Maybe you use a CSV format try to use Xlsx, I think styling in CSV is not working.

I think this is already issued and there's no solution yet. I'm also looking for the solution but the solution i only found is not use CSV.

https://github.com/PHPOffice/PhpSpreadsheet/issues/2230

  • Yes I'm using csv format, I'm making a utility for some program and the program only accepts csv format @Alfredo – Dmathz Oct 12 '22 at 02:39
  • MS Excel's CSV loader will always convert a numeric string into a number value when the csv file is opened in Excel; that's behavior that only MS could change. The only way to retain that formatting is to use the Data/Get&Transform/FromCSV option inside MS Excel that allows you to specify the rules for import of the different columns -This is issue in their github and I'm also looking for this solution. you can try this and there's no guarantee that this will work $worksheet->getCell('A1')->setValueExplicit( '0000', \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING ); – Alfredo Ramirez Jr. Oct 12 '22 at 02:48
  • Thanks for the reply bro but it's still not working. – Dmathz Oct 12 '22 at 02:53
  • This is also my problem ahahhaha, But I don't see any solution in this and almost tried everything maybe someone know the solution. – Alfredo Ramirez Jr. Oct 12 '22 at 02:57