I\'m working on a script that generate some Excel documents and I need to convert a number into its column name equivalent. For example:
1 => A
2 => B
Using PhpSpreadsheet (PHPExcel is deprecated)
// result = 'A'
\PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex(1);
Note index 0 results in 'Z'
https://phpspreadsheet.readthedocs.io/en/develop/
The correct answer (if you use PHPExcel Library) is:
// result = 'A'
$columnLetter = PHPExcel_Cell::stringFromColumnIndex(0); // ZERO-based!
and backwards:
// result = 1
$colIndex = PHPExcel_Cell::columnIndexFromString('A');