Getting Cell as String in PHPExcel by column and row

前端 未结 2 833
故里飘歌
故里飘歌 2021-01-22 12:07

I am trying to read a cell with possible trailing zeros as a string instead of numeric (which strips off leading zeros). The cell is read by integer column/row as below instead

相关标签:
2条回答
  • 2021-01-22 12:18
    $keyCell = $sheet->getCellByColumnAndRow(1,5)->getValue();
    

    Will read the cell data in the format that it's actually stored by Excel, you can't arbitrarily change that or tell PHPExcel to read it as a different datatype.

    However, if the cell has formatting applied, then you can use

    $keyCell = $sheet->getCellByColumnAndRow(1,5)->getFormattedValue();
    

    instead, and this will return the data as a string, with whatever format mask was defined in the Excel spreadsheet

    0 讨论(0)
  • 2021-01-22 12:33

    Same issue for me. I become crazy. Tried to set

     $objReader->setReadDataOnly(true);
    

    wasn't working

    tried

     $sheet->getCellByColumnAndRow(4,$row)->getValue()
    

    because normaly display text as raw => doesn't working.

    So last I change code in library. Edit file named DefaultValueBinder.php Search for dataTypeForValue function and set this :

        } elseif (is_float($pValue) || is_int($pValue)) {
            return PHPExcel_Cell_DataType::TYPE_STRING;//TYPE_NUMERIC patch here;
    
        } elseif (preg_match('/^\-?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)$/', $pValue)) {
            return PHPExcel_Cell_DataType::TYPE_STRING;//TYPE_NUMERIC patch here;
    

    So now return numbers with 0

    0 讨论(0)
提交回复
热议问题