getHighestDataColumn on phpexcelspreadsheet as count in php [duplicate]

折月煮酒 提交于 2020-08-10 18:50:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!