如何将工作表的列数作为整数(28)而不是Excel-字母("AB")?

Edw*_*uay 32 php phpexcel

鉴于:

$this->objPHPExcelReader = PHPExcel_IOFactory::createReaderForFile($this->config['file']);
$this->objPHPExcelReader->setLoadSheetsOnly(array($this->config['worksheet']));
$this->objPHPExcelReader->setReadDataOnly(true);
$this->objPHPExcel = $this->objPHPExcelReader->load($this->config['file']);
Run Code Online (Sandbox Code Playgroud)

我可以迭代这样的行,但它很慢,即在3MB的Excel文件中,工作表有"EL"列,每行大约需要1秒:

foreach ($this->objPHPExcel->setActiveSheetIndex(0)->getRowIterator() as $row)
{
    $dataset = array();
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);
    foreach ($cellIterator as $cell)
    {
        if (!is_null($cell))
        {
            $dataset[] = $cell->getCalculatedValue();
        }
    }
    $this->datasets[] = $dataset;
}
Run Code Online (Sandbox Code Playgroud)

当我像这样迭代时,它显着更快(在30秒内大约2000行),但我必须将字母例如"EL"转换为数字:

$highestColumm = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); // e.g. "EL"
$highestRow = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

$number_of_columns = 150; // TODO: figure out how to get the number of cols as int
for ($row = 1; $row < $highestRow + 1; $row++) {
    $dataset = array();
    for ($column = 0; $column < $number_of_columns; $column++) {
        $dataset[] = $this->objPHPExcel->setActiveSheetIndex(0)->getCellByColumnAndRow($column, $row)->getValue();
    }
    $this->datasets[] = $dataset;
}
Run Code Online (Sandbox Code Playgroud)

有没有办法将最高列作为整数(例如"28")而不是Excel样式的字母(例如"AB")?

Mar*_*ker 72

$colNumber = PHPExcel_Cell::columnIndexFromString($colString);
Run Code Online (Sandbox Code Playgroud)

从'A'的$ colString返回1,从'Z'返回26,从'AA'返回27,等等.

和(几乎)相反

$colString = PHPExcel_Cell::stringFromColumnIndex($colNumber);
Run Code Online (Sandbox Code Playgroud)

从$ colNumber返回'A',从25返回'Z',从26返回'AA'等.

编辑

一些有用的技巧:

工作表类有一个toArray()方法:

$this->datasets = $this->objPHPExcel->setActiveSheetIndex(0)->toArray();
Run Code Online (Sandbox Code Playgroud)

它接受以下参数:

* @param  mixed    $nullValue          Value returned in the array entry if a cell doesn't exist
* @param  boolean  $calculateFormulas  Should formulas be calculated?
* @param  boolean  $formatData         Should formatting be applied to cell values?
* @param  boolean  $returnCellRef      False - Return a simple array of rows and columns indexed by number counting from zero
*                                      True - Return rows and columns indexed by their actual row and column IDs
Run Code Online (Sandbox Code Playgroud)

虽然它确实使用了迭代器,但速度稍慢

要么

利用PHP 增加字符串Perl Style的能力

$highestColumm = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestColumn(); // e.g. "EL" 
$highestRow = $this->objPHPExcel->setActiveSheetIndex(0)->getHighestRow();  

$highestColumm++;
for ($row = 1; $row < $highestRow + 1; $row++) {     
    $dataset = array();     
    for ($column = 'A'; $column != $highestColumm; $column++) {
        $dataset[] = $this->objPHPExcel->setActiveSheetIndex(0)->getCell($column . $row)->getValue();
    }
    $this->datasets[] = $dataset;
}
Run Code Online (Sandbox Code Playgroud)

如果你正在处理大量的行,你可能会注意到$ $行的性能提高超过$ row ++

  • 谢谢,马克,我选择了第二个解决方案,它的工作非常快,非常好.对于使用字母实现这个for/next循环的任何人,请注意`$ highestColumn ++`以便你也得到最后一列,并注意它是`$ column!= $ highestColumn`而不是`$ column <= $ highestColumn `这是欺骗性的,因为它可以使用单个字母,但如果你的`$ highestColumn`是例如'EL`那么你的循环将变为`ABCDE`并停在那里. (2认同)