问题
I am using https://phpspreadsheet.readthedocs.io/en/latest/ phpspreadsheet to import files to database.I need to get the total number of columns in the uploaded excel before importing it to database.
I found getHighestDataColumn() which returns the highest coulmn as alphabets.
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$high=$spreadsheet->setActiveSheetIndex(0)->getHighestColumn();
dump($high);
die;
which gives output as I
Is there any way to get it as numbers ..??
回答1:
If you only have A-Z you can convert the chars to ascii and get their int value using ord.
// Get ascii offset for alphabet
$start = ord("A");
$current = "I";
// Calculate char position from offset A
echo ord($current) - $start;
回答2:
Found the solution as
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
dump($highestColumnIndex);
die;
This will output the index of highestColumn ..
来源:https://stackoverflow.com/questions/62877428/gethighestdatacolumn-on-phpexcelspreadsheet-as-count-in-php