0

I'm using PHPExcel reader for read data from Exce file in my Yii2 application. This is the code that I've used:

$objPHPExcel = new \PHPExcel();
        $fileName = Yii::getAlias('@webroot/trash/trash_vatout/') . $name;
        $inputFiles = fopen(Yii::getAlias('@webroot/trash/trash_vatout/') . $name, "r");
        try {
            $inputFileType = \PHPExcel_IOFactory::identify($fileName);
            $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($fileName);
        } catch (Exception $ex) {
            die('Error');
        }
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestDataRow();
        $highestColumn = $sheet->getHighestDataColumn();
        $colNumber = PHPExcel_Cell::columnIndexFromString($highestColumn);
        $col = $colNumber - 1;
        $arrayData = [];

   $bool1 = NULL;            //first bool value
   $bool2 = NULL;            //second bool value
   $bool3 = NULL;            //third bool value
   for ($row = 1; $row <= $highestRow; ++$row) {
     $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, $bool1, $bool2, $bool3);
     if (!is_null($rowData[0][$col])) {
        $arrayData[] = array_map(function($values) {
           $tempArrayKey = [];
           foreach ($values as $key => $value) {
               $newKey = $key + 1;
               $tempArrayKey[] = $newKey . '_' . $value;
           }
           return $tempArrayKey;
     }, $rowData);
   }
  }

I used it following tutorial from some source. In line code $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, $bool1, $bool2, $bool3);, it has been set three boolean value. In my case I set them all as NULL.

Anyone knows what are the purpose of the bool value actually?

I've tried manytimes to read file, if I'm not wrong, the second bool value is set for read Excel Formula.

But how about the others?

Thanks.

Blackjack
  • 1,016
  • 1
  • 20
  • 51
  • 2
    Hello there, i see you just begin beautiful journey into PHP language. Please don't forget to read doc's sometimes, something like this: http://hitautodestruct.github.io/PHPExcelAPIDocs/classes/PHPExcel_Worksheet.html#method_rangeToArray where all is explained one by one. – Yupik Mar 07 '17 at 10:27
  • Oh, i forgot about one thing. You have to click "Create array from a range of cells" title to expand specification. Have fun friend! – Yupik Mar 07 '17 at 10:31

1 Answers1

0

The signature for the rangeToArray() method is

/**
 * Create array from a range of cells
 *
 * @param string $pRange Range of cells (i.e. "A1:B10"), or just one cell (i.e. "A1")
 * @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
 * @return array
 */

so

  • $bool1 - mixed $nullValue Value returned in the array entry if a cell doesn't exist (can be any datatype/value)
  • $bool2 - boolean $calculateFormulas Should formulas be calculated?
  • $bool3 - boolean $formatData Should formatting be applied to cell values?
Mark Baker
  • 209,507
  • 32
  • 346
  • 385