Example #1
0
	/**
	 * Generate row
	 *
	 * @param	PHPExcel_Worksheet 	$pSheet			PHPExcel_Worksheet
	 * @param	array				$pValues		Array containing cells in a row
	 * @param	int					$pRow			Row number
	 * @return	string
	 * @throws	Exception
	 */
	private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) {
		if (is_array($pValues)) {
			// Construct HTML
			$html = '';

			// Write row start
			$html .= '        <tr class="row' . $pRow . '">' . "\r\n";

			// Write cells
			$colNum = 0;
			foreach ($pValues as $cell) {
				$cellData = '&nbsp;';
				$cssClass = 'column' . $colNum;
				$colSpan = 1;
				$rowSpan = 1;
				$writeCell = true;	// Write cell

				// PHPExcel_Cell
				if ($cell instanceof PHPExcel_Cell) {
					// Value
					if ($cell->getValue() instanceof PHPExcel_RichText) {
						// Loop trough rich text elements
						$elements = $cell->getValue()->getRichTextElements();
						foreach ($elements as $element) {
							// Rich text start?
							if ($element instanceof PHPExcel_RichText_Run) {
								$cellData .= '<span style="' .
									str_replace("\r\n", '',
										$this->_createCSSStyleFont($element->getFont())
									) . '">';
							}

							// Decode UTF8 data
							$cellText = $element->getText();
							if (PHPExcel_Shared_String::IsUTF8($cellText)) {
								$cellData .= utf8_decode($cellText);
							}

							if ($element instanceof PHPExcel_RichText_Run) {
								$cellData .= '</span>';
							}
						}
					} else {
						if ($this->_preCalculateFormulas) {
							$cellData = PHPExcel_Style_NumberFormat::toFormattedString(
								$cell->getCalculatedValue(),
								$pSheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
							);
						} else {
							$cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
								$cell->getValue(),
								$pSheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
							);
						}

						// Decode UTF8 data
						if (PHPExcel_Shared_String::IsUTF8($cellData)) {
							$cellData = utf8_decode($cellData);
						}
					}

					// Check value
					if ($cellData == '') {
						$cellData = '&nbsp;';
					}

					// Extend CSS class?
					if (array_key_exists($cell->getCoordinate(), $pSheet->getStyles())) {
						$cssClass .= ' style' . $pSheet->getStyle($cell->getCoordinate())->getHashCode();
					}
				} else {
					$cell = new PHPExcel_Cell(
						PHPExcel_Cell::stringFromColumnIndex($colNum),
						($pRow + 1),
						'',
						null,
						null
					);
				}

				// Hyperlink?
				if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
					$cellData = '<a href="' . $cell->getHyperlink()->getUrl() . '" title="' . $cell->getHyperlink()->getTooltip() . '">' . $cellData . '</a>';
				}

				// Column/rowspan
				foreach ($pSheet->getMergeCells() as $cells) {
					if ($cell->isInRange($cells)) {
						list($first, ) = PHPExcel_Cell::splitRange($cells);

						if ($first == $cell->getCoordinate()) {
							list($colSpan, $rowSpan) = PHPExcel_Cell::rangeDimension($cells);
						} else {
							$writeCell = false;
						}

						break;
					}
				}

				// Write
				if ($writeCell) {
					// Column start
					$html .= '          <td';
						$html .= ' class="' . $cssClass . '"';
						if ($colSpan > 1) {
							$html .= ' colspan="' . $colSpan . '"';
						}
						if ($rowSpan > 1) {
							$html .= ' rowspan="' . $rowSpan . '"';
						}
					$html .= '>';

					// Image?
					$html .= $this->_writeImageTagInCell($pSheet, $cell->getCoordinate());

					// 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
 /**
  * Write line to CSV file
  *
  * @param	mixed	$pFileHandle	PHP filehandle
  * @param	array	$pValues		Array containing values in a row
  * @throws	Exception
  */
 private function _writeLine($pFileHandle = null, $pValues = null)
 {
     if (!is_null($pFileHandle) && is_array($pValues)) {
         // No leading delimiter
         $writeDelimiter = false;
         // Build the line
         $line = '';
         foreach ($pValues as $element) {
             // Decode UTF8 data
             if (PHPExcel_Shared_String::IsUTF8($element)) {
                 $element = utf8_decode($element);
             }
             // Escape enclosures
             $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
             // Add delimiter
             if ($writeDelimiter) {
                 $line .= $this->_delimiter;
             } else {
                 $writeDelimiter = true;
             }
             // Add enclosed string
             $line .= $this->_enclosure . $element . $this->_enclosure;
         }
         // Add line ending
         $line .= $this->_lineEnding;
         // Write to file
         fwrite($pFileHandle, $line);
     } else {
         throw new Exception("Invalid parameters passed.");
     }
 }