Example #1
0
 /**
  * Write styles to file
  * 
  * @param	mixed				$pFileHandle	PHP filehandle
  * @param	PHPExcel_Worksheet 	$pSheet			PHPExcel_Worksheet
  * @throws	Exception
  */
 private function _writeStyles($pFileHandle = null, PHPExcel_Worksheet $pSheet)
 {
     if (!is_null($pFileHandle)) {
         // Construct HTML
         $html = '';
         // Start styles
         $html .= '    <style>' . "\r\n";
         $html .= '    <!--' . "\r\n";
         $html .= '      html {' . "\r\n";
         $html .= '        font-family: Calibri, Arial, Helvetica, Sans Serif;' . "\r\n";
         $html .= '        font-size: 10pt;' . "\r\n";
         $html .= '        background-color: white;' . "\r\n";
         $html .= '      }' . "\r\n";
         $html .= '      table.sheet, table.sheet td {' . "\r\n";
         if ($pSheet->getShowGridlines()) {
             $html .= '        border: 1px dotted black;' . "\r\n";
         }
         $html .= '      }' . "\r\n";
         // Calculate column widths
         $pSheet->calculateColumnWidths();
         foreach ($pSheet->getColumnDimensions() as $columnDimension) {
             $column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
             $html .= '      td.column' . $column . ' {' . "\r\n";
             $html .= '        width: ' . PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) . 'px;' . "\r\n";
             if ($columnDimension->getVisible() === false) {
                 $html .= '        display: none;' . "\r\n";
                 $html .= '        visibility: hidden;' . "\r\n";
             }
             $html .= '      }' . "\r\n";
         }
         // Calculate row heights
         foreach ($pSheet->getRowDimensions() as $rowDimension) {
             $html .= '      tr.row' . ($rowDimension->getRowIndex() - 1) . ' {' . "\r\n";
             // height is disproportionately large
             $px_height = round(PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) / 12);
             $html .= '        height: ' . $px_height . 'px;' . "\r\n";
             if ($rowDimension->getVisible() === false) {
                 $html .= '        display: none;' . "\r\n";
                 $html .= '        visibility: hidden;' . "\r\n";
             }
             $html .= '      }' . "\r\n";
         }
         // Calculate cell style hashes
         $cellStyleHashes = new PHPExcel_HashTable();
         $cellStyleHashes->addFromSource($pSheet->getStyles());
         for ($i = 0; $i < $cellStyleHashes->count(); $i++) {
             $html .= $this->_createCSSStyle($cellStyleHashes->getByIndex($i));
         }
         // End styles
         $html .= '    -->' . "\r\n";
         $html .= '    </style>' . "\r\n";
         // Write to file
         fwrite($pFileHandle, $html);
     } else {
         throw new Exception("Invalid parameters passed.");
     }
 }
Example #2
0
	/**
	 * Generate CSS styles
	 *
	 * @param	boolean	$generateSurroundingHTML	Generate surrounding HTML tags? (<style> and </style>)
	 * @return	string
	 * @throws	Exception
	 */
	public function generateStyles($generateSurroundingHTML = true) {
		// PHPExcel object known?
		if (is_null($this->_phpExcel)) {
			throw new Exception('Internal PHPExcel object not set to an instance of an object.');
		}

		// Construct HTML
		$html = '';

		// Start styles
		if ($generateSurroundingHTML) {
			$html .= '    <style>' . "\r\n";
			$html .= '    <!--' . "\r\n";
			$html .= '      html {' . "\r\n";
			$html .= '        font-family: Calibri, Arial, Helvetica, Sans Serif;' . "\r\n";
			$html .= '        font-size: 10pt;' . "\r\n";
			$html .= '        background-color: white;' . "\r\n";
			$html .= '      }' . "\r\n";
		}

		// Write styles per sheet
		foreach ($this->_phpExcel->getAllSheets() as $sheet) {
			// Calculate hash code
			$hashCode = $sheet->getHashCode();

			// Write styles
			$html .= '      table.sheet' . $hashCode . ', table.sheet' . $hashCode . ' td {' . "\r\n";
			if ($sheet->getShowGridlines()) {
				$html .= '        border: 1px dotted black;' . "\r\n";
			}
			$html .= '        page-break-after: always;' . "\r\n";
			$html .= '      }' . "\r\n";

			// Default column width
			$columnDimension = $sheet->getDefaultColumnDimension();

			$html .= '      table.sheet' . $hashCode . ' td {' . "\r\n";
			$html .= '        width: ' . PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) . 'px;' . "\r\n";
			if ($columnDimension->getVisible() === false) {
				$html .= '        display: none;' . "\r\n";
				$html .= '        visibility: hidden;' . "\r\n";
			}
			$html .= '      }' . "\r\n";

			// Calculate column widths
			$sheet->calculateColumnWidths();
			foreach ($sheet->getColumnDimensions() as $columnDimension) {
				$column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;

				$html .= '      table.sheet' . $hashCode . ' td.column' . $column  . ' {' . "\r\n";
				$html .= '        width: ' . PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) . 'px;' . "\r\n";
				if ($columnDimension->getVisible() === false) {
					$html .= '        display: none;' . "\r\n";
					$html .= '        visibility: hidden;' . "\r\n";
				}
				$html .= '      }' . "\r\n";
			}

			// Default row height
			$rowDimension = $sheet->getDefaultRowDimension();

			$html .= '      table.sheet' . $hashCode . ' tr {' . "\r\n";
			// height is disproportionately large
			$px_height = round( PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) / 12 );
			$html .= '        height: ' . $px_height . 'px;' . "\r\n";
			if ($rowDimension->getVisible() === false) {
				$html .= '        display: none;' . "\r\n";
				$html .= '        visibility: hidden;' . "\r\n";
			}
			$html .= '      }' . "\r\n";

			// Calculate row heights
			foreach ($sheet->getRowDimensions() as $rowDimension) {
				$html .= '      table.sheet' . $hashCode . ' tr.row' . ($rowDimension->getRowIndex() - 1)  . ' {' . "\r\n";
				// height is disproportionately large
				$px_height = round( PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) / 12 );
				$html .= '        height: ' . $px_height . 'px;' . "\r\n";
				if ($rowDimension->getVisible() === false) {
					$html .= '        display: none;' . "\r\n";
					$html .= '        visibility: hidden;' . "\r\n";
				}
				$html .= '      }' . "\r\n";
			}

			// Calculate cell style hashes
			$cellStyleHashes = new PHPExcel_HashTable();
			$cellStyleHashes->addFromSource( $sheet->getStyles() );
			for ($i = 0; $i < $cellStyleHashes->count(); $i++) {
				$html .= $this->_createCSSStyle( $cellStyleHashes->getByIndex($i) );
			}
		}

		// End styles
		if ($generateSurroundingHTML) {
			$html .= '    -->' . "\r\n";
			$html .= '    </style>' . "\r\n";
		}

		// Return
		return $html;
	}