Example #1
0
 /**
  * Generate row
  *
  * @param	Worksheet 	$pSheet			Worksheet
  * @param	array				$pValues		Array containing cells in a row
  * @param	int					$pRow			Row number (0-based)
  * @return	string
  * @throws	Exception
  */
 private function _generateRow(Worksheet $pSheet, $pValues = null, $pRow = 0)
 {
     if (is_array($pValues)) {
         // Construct HTML
         $html = '';
         // Sheet index
         $sheetIndex = $pSheet->getParent()->getIndex($pSheet);
         // TCPDF and breaks
         if ($this->_isPdf && count($pSheet->getBreaks()) > 0) {
             $breaks = $pSheet->getBreaks();
             // check if a break is needed before this row
             if (isset($breaks['A' . $pRow])) {
                 // close table: </table>
                 $html .= $this->_generateTableFooter();
                 // insert page break
                 $html .= '<tcpdf method="AddPage" />';
                 // open table again: <table> + <col> etc.
                 $html .= $this->_generateTableHeader($pSheet);
             }
         }
         // Write row start
         if (!$this->_useInlineCss) {
             $html .= '          <tr class="row' . $pRow . '">' . "\r\n";
         } else {
             $style = isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) ? $this->_assembleCSS($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]) : '';
             $html .= '          <tr style="' . $style . '">' . "\r\n";
         }
         // Write cells
         $colNum = 0;
         foreach ($pValues as $cell) {
             $coordinate = Cell::stringFromColumnIndex($colNum) . ($pRow + 1);
             if (!$this->_useInlineCss) {
                 $cssClass = '';
                 $cssClass = 'column' . $colNum;
             } else {
                 $cssClass = array();
                 if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum])) {
                     $this->_cssStyles['table.sheet' . $sheetIndex . ' td.column' . $colNum];
                 }
             }
             $colSpan = 1;
             $rowSpan = 1;
             $writeCell = true;
             // Write cell
             // initialize
             $cellData = '';
             // Cell
             if ($cell instanceof Cell) {
                 if (is_null($cell->getParent())) {
                     $cell->attach($pSheet);
                 }
                 // Value
                 if ($cell->getValue() instanceof RichText) {
                     // Loop through rich text elements
                     $elements = $cell->getValue()->getRichTextElements();
                     foreach ($elements as $element) {
                         // Rich text start?
                         if ($element instanceof RichText_Run) {
                             $cellData .= '<span style="' . $this->_assembleCSS($this->_createCSSStyleFont($element->getFont())) . '">';
                             if ($element->getFont()->getSuperScript()) {
                                 $cellData .= '<sup>';
                             } else {
                                 if ($element->getFont()->getSubScript()) {
                                     $cellData .= '<sub>';
                                 }
                             }
                         }
                         // Convert UTF8 data to PCDATA
                         $cellText = $element->getText();
                         $cellData .= htmlspecialchars($cellText);
                         if ($element instanceof RichText_Run) {
                             if ($element->getFont()->getSuperScript()) {
                                 $cellData .= '</sup>';
                             } else {
                                 if ($element->getFont()->getSubScript()) {
                                     $cellData .= '</sub>';
                                 }
                             }
                             $cellData .= '</span>';
                         }
                     }
                 } else {
                     if ($this->_preCalculateFormulas) {
                         $cellData = Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), $pSheet->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode(), array($this, 'formatColor'));
                     } else {
                         $cellData = Style_NumberFormat::ToFormattedString($cell->getValue(), $pSheet->getParent()->getCellXfByIndex($cell->getXfIndex())->getNumberFormat()->getFormatCode(), array($this, 'formatColor'));
                     }
                 }
                 // replace leading spaces on each line with &nbsp;
                 $cellData = $this->_convertNbsp($cellData);
                 // convert newline "\n" to '<br>'
                 $cellData = str_replace("\n", '<br/>', $cellData);
                 // Extend CSS class?
                 if (!$this->_useInlineCss) {
                     $cssClass .= ' style' . $cell->getXfIndex();
                     $cssClass .= ' ' . $cell->getDataType();
                 } else {
                     if (isset($this->_cssStyles['td.style' . $cell->getXfIndex()])) {
                         $cssClass = array_merge($cssClass, $this->_cssStyles['td.style' . $cell->getXfIndex()]);
                     }
                     // General horizontal alignment: Actual horizontal alignment depends on dataType
                     $sharedStyle = $pSheet->getParent()->getCellXfByIndex($cell->getXfIndex());
                     if ($sharedStyle->getAlignment()->getHorizontal() == Style_Alignment::HORIZONTAL_GENERAL && isset($this->_cssStyles['.' . $cell->getDataType()]['text-align'])) {
                         $cssClass['text-align'] = $this->_cssStyles['.' . $cell->getDataType()]['text-align'];
                     }
                 }
             }
             // Hyperlink?
             if ($pSheet->hyperlinkExists($coordinate) && !$pSheet->getHyperlink($coordinate)->isInternal()) {
                 $cellData = '<a href="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getUrl()) . '" title="' . htmlspecialchars($pSheet->getHyperlink($coordinate)->getTooltip()) . '">' . $cellData . '</a>';
             }
             // Should the cell be written or is it swallowed by a rowspan or colspan?
             $writeCell = !(isset($this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum]) && $this->_isSpannedCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum]);
             // Colspan and Rowspan
             $colspan = 1;
             $rowspan = 1;
             if (isset($this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum])) {
                 $spans = $this->_isBaseCell[$pSheet->getParent()->getIndex($pSheet)][$pRow + 1][$colNum];
                 $rowSpan = $spans['rowspan'];
                 $colSpan = $spans['colspan'];
             }
             // Write
             if ($writeCell) {
                 // Column start
                 $html .= '            <td';
                 if (!$this->_useInlineCss) {
                     $html .= ' class="' . $cssClass . '"';
                 } else {
                     //** Necessary redundant code for the sake of Writer_PDF **
                     // We must explicitly write the width of the <td> element because TCPDF
                     // does not recognize e.g. <col style="width:42pt">
                     $width = 0;
                     for ($i = $colNum; $i < $colNum + $colSpan; ++$i) {
                         if (isset($this->_columnWidths[$sheetIndex][$i])) {
                             $width += $this->_columnWidths[$sheetIndex][$i];
                         }
                     }
                     $cssClass['width'] = $width . 'pt';
                     // We must also explicitly write the height of the <td> element because TCPDF
                     // does not recognize e.g. <tr style="height:50pt">
                     if (isset($this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'])) {
                         $height = $this->_cssStyles['table.sheet' . $sheetIndex . ' tr.row' . $pRow]['height'];
                         $cssClass['height'] = $height;
                     }
                     //** end of redundant code **
                     $html .= ' style="' . $this->_assembleCSS($cssClass) . '"';
                 }
                 if ($colSpan > 1) {
                     $html .= ' colspan="' . $colSpan . '"';
                 }
                 if ($rowSpan > 1) {
                     $html .= ' rowspan="' . $rowSpan . '"';
                 }
                 $html .= '>';
                 // Image?
                 $html .= $this->_writeImageTagInCell($pSheet, $coordinate);
                 // Cell data
                 $html .= $cellData;
                 // Column end
                 $html .= '</td>' . "\r\n";
             }
             // Next column
             ++$colNum;
         }
         // Write row end
         $html .= '          </tr>' . "\r\n";
         // Return
         return $html;
     } else {
         throw new Exception("Invalid parameters passed.");
     }
 }
Example #2
0
 /**
  * Create array from worksheet
  *
  * @param mixed $nullValue Value treated as "null"
  * @param boolean $calculateFormulas Should formulas be calculated?
  * @return array
  */
 public function toArray($nullValue = null, $calculateFormulas = true)
 {
     // Returnvalue
     $returnValue = array();
     // Garbage collect...
     $this->garbageCollect();
     // Get worksheet dimension
     $dimension = explode(':', $this->calculateWorksheetDimension());
     $dimension[0] = Cell::coordinateFromString($dimension[0]);
     $dimension[0][0] = Cell::columnIndexFromString($dimension[0][0]) - 1;
     $dimension[1] = Cell::coordinateFromString($dimension[1]);
     $dimension[1][0] = Cell::columnIndexFromString($dimension[1][0]) - 1;
     // Loop through cells
     for ($row = $dimension[0][1]; $row <= $dimension[1][1]; ++$row) {
         for ($column = $dimension[0][0]; $column <= $dimension[1][0]; ++$column) {
             // Cell exists?
             if ($this->cellExistsByColumnAndRow($column, $row)) {
                 $cell = $this->getCellByColumnAndRow($column, $row);
                 if ($cell->getValue() instanceof RichText) {
                     $returnValue[$row][$column] = $cell->getValue()->getPlainText();
                 } else {
                     if ($calculateFormulas) {
                         $returnValue[$row][$column] = $cell->getCalculatedValue();
                     } else {
                         $returnValue[$row][$column] = $cell->getValue();
                     }
                 }
                 $style = $this->_parent->getCellXfByIndex($cell->getXfIndex());
                 $returnValue[$row][$column] = Style_NumberFormat::toFormattedString($returnValue[$row][$column], $style->getNumberFormat()->getFormatCode());
             } else {
                 $returnValue[$row][$column] = $nullValue;
             }
         }
     }
     // Return
     return $returnValue;
 }
Example #3
0
 /**
  *	TEXTFORMAT
  *
  *	@param	mixed	$value	Value to check
  *	@return	boolean
  */
 public static function TEXTFORMAT($value, $format)
 {
     $value = self::flattenSingleValue($value);
     $format = self::flattenSingleValue($format);
     if (is_string($value) && !is_numeric($value) && Shared_Date::isDateTimeFormatCode($format)) {
         $value = self::DATEVALUE($value);
     }
     return (string) Style_NumberFormat::toFormattedString($value, $format);
 }