コード例 #1
0
ファイル: Worksheet.php プロジェクト: marvinchau/sss_server
 /**
  * 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
  */
 public function rangeToArray($pRange = 'A1', $nullValue = null, $calculateFormulas = true, $formatData = true, $returnCellRef = false)
 {
     // Returnvalue
     $returnValue = array();
     //    Identify the range that we need to extract from the worksheet
     list($rangeStart, $rangeEnd) = PHPExcel_Cell::rangeBoundaries($pRange);
     $minCol = PHPExcel_Cell::stringFromColumnIndex($rangeStart[0] - 1);
     $minRow = $rangeStart[1];
     $maxCol = PHPExcel_Cell::stringFromColumnIndex($rangeEnd[0] - 1);
     $maxRow = $rangeEnd[1];
     $maxCol++;
     // Loop through rows
     $r = -1;
     for ($row = $minRow; $row <= $maxRow; ++$row) {
         $rRef = $returnCellRef ? $row : ++$r;
         $c = -1;
         // Loop through columns in the current row
         for ($col = $minCol; $col != $maxCol; ++$col) {
             $cRef = $returnCellRef ? $col : ++$c;
             //    Using getCell() will create a new cell if it doesn't already exist. We don't want that to happen
             //        so we test and retrieve directly against _cellCollection
             if ($this->_cellCollection->isDataSet($col . $row)) {
                 // Cell exists
                 $cell = $this->_cellCollection->getCacheData($col . $row);
                 if ($cell->getValue() !== null) {
                     if ($cell->getValue() instanceof PHPExcel_RichText) {
                         $returnValue[$rRef][$cRef] = $cell->getValue()->getPlainText();
                     } else {
                         if ($calculateFormulas) {
                             $returnValue[$rRef][$cRef] = $cell->getCalculatedValue();
                         } else {
                             $returnValue[$rRef][$cRef] = $cell->getValue();
                         }
                     }
                     if ($formatData) {
                         $style = $this->_parent->getCellXfByIndex($cell->getXfIndex());
                         $returnValue[$rRef][$cRef] = PHPExcel_Style_NumberFormat::toFormattedString($returnValue[$rRef][$cRef], $style && $style->getNumberFormat() ? $style->getNumberFormat()->getFormatCode() : PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
                     }
                 } else {
                     // Cell holds a NULL
                     $returnValue[$rRef][$cRef] = $nullValue;
                 }
             } else {
                 // Cell doesn't exist
                 $returnValue[$rRef][$cRef] = $nullValue;
             }
         }
     }
     // Return
     return $returnValue;
 }