/** * Write row to HTML file * * @param mixed $pFileHandle PHP filehandle * @param PHPExcel_Worksheet $pSheet PHPExcel_Worksheet * @param array $pValues Array containing cells in a row * @param int $pRow Row number * @throws Exception */ private function _writeRow($pFileHandle = null, PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) { if (!is_null($pFileHandle) && is_array($pValues)) { // Write row start fwrite($pFileHandle, ' <tr class="row' . $pRow . '">' . "\r\n"); // Write cells $colNum = 0; foreach ($pValues as $cell) { $cellData = ' '; $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())) . '">'; } $cellData .= $element->getText(); 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()); } } // Check value if ($cellData == '') { $cellData = ' '; } // 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 fwrite($pFileHandle, ' <td'); fwrite($pFileHandle, ' class="' . $cssClass . '"'); if ($colSpan > 1) { fwrite($pFileHandle, ' colspan="' . $colSpan . '"'); } if ($rowSpan > 1) { fwrite($pFileHandle, ' rowspan="' . $rowSpan . '"'); } fwrite($pFileHandle, '>'); // Image? $this->_writeImageInCell($pFileHandle, $pSheet, $cell->getCoordinate()); // Cell data fwrite($pFileHandle, $cellData); // Column end fwrite($pFileHandle, '</td>' . "\r\n"); } // Next column $colNum++; } // Write row end fwrite($pFileHandle, ' </tr>' . "\r\n"); } else { throw new Exception("Invalid parameters passed."); } }
/** * Add data to the beginning of the workbook (note the reverse order) * and to the end of the workbook. * * @access public * @see PHPExcel_Writer_Excel5_Workbook::storeWorkbook() */ function close() { $num_sheets = count($this->_phpSheet->getParent()->getAllSheets()); // Write BOF record $this->_storeBof(0x10); // Write DEFCOLWIDTH record $this->_storeDefcol(); // Calculate column widths $this->_phpSheet->calculateColumnWidths(); // Column dimensions foreach ($this->_phpSheet->getColumnDimensions() as $columnDimension) { $column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1; if ($column < 256) { if ($columnDimension->getWidth() >= 0) { $width = $columnDimension->getWidth(); } else { if ($this->_phpSheet->getDefaultColumnDimension()->getWidth() >= 0) { $width = $this->_phpSheet->getDefaultColumnDimension()->getWidth(); } else { $width = 8; } } $this->_setColumn($column, $column, $width, null, $columnDimension->getVisible() ? '0' : '1', $columnDimension->getOutlineLevel()); } } // Write the COLINFO records if they exist if (!empty($this->_colinfo)) { $colcount = count($this->_colinfo); for ($i = 0; $i < $colcount; ++$i) { $this->_storeColinfo($this->_colinfo[$i]); } } // Write EXTERNCOUNT of external references if ($this->_BIFF_version == 0x500) { $this->_storeExterncount($num_sheets); } // Write EXTERNSHEET references if ($this->_BIFF_version == 0x500) { for ($i = 0; $i < $num_sheets; ++$i) { $this->_storeExternsheet($this->_phpSheet->getParent()->getSheet($i)->getTitle()); } } // Write PRINTHEADERS $this->_storePrintHeaders(); // Write PRINTGRIDLINES $this->_storePrintGridlines(); // Write GUTS $this->_storeGuts(); // Write GRIDSET $this->_storeGridset(); // Write DEFAULTROWHEIGHT if ($this->_BIFF_version == 0x600) { $this->_storeDefaultRowHeight(); } // Write WSBOOL $this->_storeWsbool(); // Write horizontal and vertical page breaks $this->_storeBreaks(); // Write page header $this->_storeHeader(); // Write page footer $this->_storeFooter(); // Write page horizontal centering $this->_storeHcenter(); // Write page vertical centering $this->_storeVcenter(); // Write left margin $this->_storeMarginLeft(); // Write right margin $this->_storeMarginRight(); // Write top margin $this->_storeMarginTop(); /* FIXME: margins are actually appended */ // Write bottom margin $this->_storeMarginBottom(); // Write page setup $this->_storeSetup(); // Write sheet protection $this->_storeProtect(); // Write sheet password $this->_storePassword(); // Write sheet dimensions $this->_storeDimensions(); // Write Cells $aStyles = $this->_phpSheet->getStyles(); $emptyStyle = $this->_phpSheet->getDefaultStyle(); foreach ($this->_phpSheet->getCellCollection() as $cell) { $row = $cell->getRow() - 1; $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1; // Don't break Excel! if ($row + 1 > 65536 or $column + 1 > 256) { break; } $style = $emptyStyle; if (isset($aStyles[$cell->getCoordinate()])) { $style = $aStyles[$cell->getCoordinate()]; } $styleHashIndex = $style->getHashIndex(); // Write cell value if ($cell->getValue() instanceof PHPExcel_RichText) { $this->_writeString($row, $column, $cell->getValue()->getPlainText(), $this->_xfIndexes[$styleHashIndex]); } else { switch ($cell->getDatatype()) { case PHPExcel_Cell_DataType::TYPE_STRING: if ($cell->getValue() === '' or $cell->getValue() === null) { $this->_writeBlank($row, $column, $this->_xfIndexes[$styleHashIndex]); } else { $this->_writeString($row, $column, $cell->getValue(), $this->_xfIndexes[$styleHashIndex]); } break; case PHPExcel_Cell_DataType::TYPE_FORMULA: $this->_writeFormula($row, $column, $cell->getValue(), $this->_xfIndexes[$styleHashIndex]); break; case PHPExcel_Cell_DataType::TYPE_BOOL: $this->_writeBoolErr($row, $column, $cell->getValue(), 0, $this->_xfIndexes[$styleHashIndex]); break; case PHPExcel_Cell_DataType::TYPE_ERROR: $this->_writeBoolErr($row, $column, $this->_mapErrorCode($cell->getValue()), 1, $this->_xfIndexes[$styleHashIndex]); break; default: $this->_write($row, $column, $cell->getValue(), $this->_xfIndexes[$styleHashIndex], $style->getNumberFormat()->getFormatCode()); break; } // Hyperlink? if ($cell->hasHyperlink()) { $this->_writeUrl($row, $column, str_replace('sheet://', 'internal:', $cell->getHyperlink()->getUrl())); } } } // Row dimensions foreach ($this->_phpSheet->getRowDimensions() as $rowDimension) { $this->_setRow($rowDimension->getRowIndex() - 1, $rowDimension->getRowHeight(), null, $rowDimension->getVisible() ? '0' : '1', $rowDimension->getOutlineLevel()); } // Append if ($this->_BIFF_version == 0x600) { $this->_storeMsoDrawing(); } $this->_storeWindow2(); $this->_storeZoom(); if ($this->_phpSheet->getFreezePane()) { $this->_storePanes(); } $this->_storeSelection($this->_selection); $this->_storeMergedCells(); /* TODO: add data validity */ /*if ($this->_BIFF_version == 0x0600) { $this->_storeDataValidity(); }*/ if ($this->_BIFF_version == 0x600) { $this->_storeRangeProtection(); } $this->_storeEof(); }
/** * 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 = ''; // Sheet hashcode $sheetHash = $pSheet->getHashCode(); // Write row start if (!$this->_useInlineCss) { $html .= ' <tr class="row' . $pRow . '">' . "\r\n"; } else { $style = isset($this->_cssStyles['table.sheet' . $sheetHash . ' tr.row' . $pRow]) ? $this->_cssStyles['table.sheet' . $sheetHash . ' tr.row' . $pRow] : ''; $html .= ' <tr style="' . $style . '">' . "\r\n"; } // Write cells $colNum = 0; foreach ($pValues as $cell) { $cellData = ' '; $cssClass = ''; if (!$this->_useInlineCss) { $cssClass = 'column' . $colNum; } else { $cssClass = isset($this->_cssStyles['table.sheet' . $sheetHash . ' td.column' . $colNum]) ? $this->_cssStyles['table.sheet' . $sheetHash . ' td.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())) . '">'; 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 PHPExcel_RichText_Run) { if ($element->getFont()->getSuperScript()) { $cellData .= '</sup>'; } else { if ($element->getFont()->getSubScript()) { $cellData .= '</sub>'; } } $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()); } // Convert UTF8 data to PCDATA $cellData = htmlspecialchars($cellData); } // Check value if ($cellData == '') { $cellData = ' '; } // Extend CSS class? if (array_key_exists($cell->getCoordinate(), $pSheet->getStyles())) { if (!$this->_useInlineCss) { $cssClass .= ' style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex(); $cssClass .= ' ' . $cell->getDataType(); } else { $cssClass .= isset($this->_cssStyles['style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex()]) ? $this->_cssStyles['style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex()] : ''; // General horizontal alignment: Actual horizontal alignment depends on dataType if ($pSheet->getStyle($cell->getCoordinate())->getAlignment()->getHorizontal() == PHPExcel_Style_Alignment::HORIZONTAL_GENERAL && isset($this->_cssStyles['.' . $cell->getDataType()])) { if (preg_match('/text-align: [^;]*;/', $cssClass)) { $cssClass = preg_replace('/text-align: [^;]*;/', $this->_cssStyles['.' . $cell->getDataType()], $cssClass); } else { $cssClass .= $this->_cssStyles['.' . $cell->getDataType()]; } } } } } else { $cell = new PHPExcel_Cell(PHPExcel_Cell::stringFromColumnIndex($colNum), $pRow + 1, '', null, null); } // Hyperlink? if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) { $cellData = '<a href="' . htmlspecialchars($cell->getHyperlink()->getUrl()) . '" title="' . htmlspecialchars($cell->getHyperlink()->getTooltip()) . '">' . $cellData . '</a>'; } // Column/rowspan foreach ($pSheet->getMergeCells() as $cells) { if ($cell->isInRange($cells)) { list($first, ) = PHPExcel_Cell::splitRange($cells); if ($first[0] == $cell->getCoordinate()) { list($colSpan, $rowSpan) = PHPExcel_Cell::rangeDimension($cells); } else { $writeCell = false; } break; } } // Write if ($writeCell) { // Column start $html .= ' <td'; if (!$this->_useInlineCss) { $html .= ' class="' . $cssClass . '"'; } else { //** Necessary redundant code for the sake of PHPExcel_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; $columnIndex = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1; for ($i = $columnIndex; $i < $columnIndex + $colSpan; ++$i) { if (isset($this->_columnWidths[$sheetHash][$i])) { $width += $this->_columnWidths[$sheetHash][$i]; } } $cssClass .= 'width: ' . $width . 'pt; '; //** end of redundant code ** $html .= ' style="' . $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."); } }
/** * Write Cell * * @param PHPExcel_Shared_XMLWriter $objWriter XML Writer * @param PHPExcel_Worksheet $pSheet Worksheet * @param PHPExcel_Cell $pCell Cell * @param string[] $pStringTable String table * @param string[] $pFlippedStringTable String table (flipped), for faster index searching * @throws Exception */ private function _writeCell(PHPExcel_Shared_XMLWriter $objWriter = null, PHPExcel_Worksheet $pSheet = null, PHPExcel_Cell $pCell = null, $pStringTable = null, $pFlippedStringTable = null) { if (is_array($pStringTable) && is_array($pFlippedStringTable)) { // Cell $objWriter->startElement('c'); $objWriter->writeAttribute('r', $pCell->getCoordinate()); // Sheet styles $aStyles = $pSheet->getStyles(); if (isset($aStyles[$pCell->getCoordinate()])) { $styleIndex = $this->getParentWriter()->getStylesHashTable()->getIndexForHashCode($aStyles[$pCell->getCoordinate()]->getHashCode()); if ($styleIndex != '') { $objWriter->writeAttribute('s', $styleIndex); } } // If cell value is supplied, write cell value if (is_object($pCell->getValue()) || $pCell->getValue() !== '') { // Map type $mappedType = $pCell->getDataType(); // Write data type depending on its type switch (strtolower($mappedType)) { case 'inlinestr': // Inline string $objWriter->writeAttribute('t', $mappedType); break; case 's': // String $objWriter->writeAttribute('t', $mappedType); break; case 'b': // Boolean $objWriter->writeAttribute('t', $mappedType); break; case 'f': // Formula $calculatedValue = null; if ($this->getParentWriter()->getPreCalculateFormulas()) { $calculatedValue = $pCell->getCalculatedValue(); } else { $calculatedValue = $pCell->getValue(); } if (is_string($calculatedValue)) { $objWriter->writeAttribute('t', 'str'); } break; } // Write data depending on its type switch (strtolower($mappedType)) { case 'inlinestr': // Inline string if (!$pCell->getValue() instanceof PHPExcel_RichText) { $objWriter->writeElement('t', PHPExcel_Shared_String::ControlCharacterPHP2OOXML($pCell->getValue())); } else { if ($pCell->getValue() instanceof PHPExcel_RichText) { $objWriter->startElement('is'); $this->getParentWriter()->getWriterPart('stringtable')->writeRichText($objWriter, $pCell->getValue()); $objWriter->endElement(); } } break; case 's': // String if (!$pCell->getValue() instanceof PHPExcel_RichText) { if (isset($pFlippedStringTable[$pCell->getValue()])) { $objWriter->writeElement('v', $pFlippedStringTable[$pCell->getValue()]); } } else { if ($pCell->getValue() instanceof PHPExcel_RichText) { $objWriter->writeElement('v', $pFlippedStringTable[$pCell->getValue()->getHashCode()]); } } break; case 'f': // Formula $objWriter->writeElement('f', substr($pCell->getValue(), 1)); if ($this->getParentWriter()->getOffice2003Compatibility() === false) { if ($this->getParentWriter()->getPreCalculateFormulas()) { $calculatedValue = $pCell->getCalculatedValue(); if (substr($calculatedValue, 0, 1) != '#') { $objWriter->writeElement('v', $calculatedValue); } else { $objWriter->writeElement('v', '0'); } } else { $objWriter->writeElement('v', '0'); } } break; case 'n': // Numeric if (PHPExcel_Shared_Date::isDateTime($pCell)) { $dateValue = $pCell->getValue(); if (is_string($dateValue)) { // Error string $objWriter->writeElement('v', $pFlippedStringTable[$dateValue]); } elseif (!is_float($dateValue)) { // PHP serialized date/time or date/time object $objWriter->writeElement('v', PHPExcel_Shared_Date::PHPToExcel($dateValue)); } else { // Excel serialized date/time $objWriter->writeElement('v', $dateValue); } } else { $objWriter->writeElement('v', $pCell->getValue()); } break; case 'b': // Boolean $objWriter->writeElement('v', $pCell->getValue() ? '1' : '0'); break; } } $objWriter->endElement(); } else { throw new Exception("Invalid parameters passed."); } }
/** * 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 = ''; // Sheet hashcode $sheetHash = $pSheet->getHashCode(); // Write row start if (!$this->_useInlineCss) { $html .= ' <tr class="row' . $pRow . '">' . "\r\n"; } else { $style = isset($this->_cssStyles['table.sheet' . $sheetHash . ' tr.row' . $pRow]) ? $this->_cssStyles['table.sheet' . $sheetHash . ' tr.row' . $pRow] : ''; $html .= ' <tr style="' . $style . '">' . "\r\n"; } // Write cells $colNum = 0; foreach ($pValues as $cell) { $cellData = ' '; $cssClass = ''; if (!$this->_useInlineCss) { $cssClass = 'column' . $colNum; } else { $cssClass = isset($this->_cssStyles['table.sheet' . $sheetHash . ' td.column' . $colNum]) ? $this->_cssStyles['table.sheet' . $sheetHash . ' td.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())) . '">'; 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 PHPExcel_RichText_Run) { if ($element->getFont()->getSuperScript()) { $cellData .= '</sup>'; } else { if ($element->getFont()->getSubScript()) { $cellData .= '</sub>'; } } $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()); } // Convert UTF8 data to PCDATA $cellData = htmlspecialchars($cellData); } // Check value if ($cellData == '') { $cellData = ' '; } // Extend CSS class? if (array_key_exists($cell->getCoordinate(), $pSheet->getStyles())) { if (!$this->_useInlineCss) { $cssClass .= ' style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex(); } else { $cssClass .= isset($this->_cssStyles['style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex()]) ? $this->_cssStyles['style' . $pSheet->getStyle($cell->getCoordinate())->getHashIndex()] : ''; } } } else { $cell = new PHPExcel_Cell(PHPExcel_Cell::stringFromColumnIndex($colNum), $pRow + 1, '', null, null); } // Hyperlink? if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) { $cellData = '<a href="' . htmlspecialchars($cell->getHyperlink()->getUrl()) . '" title="' . htmlspecialchars($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'; if (!$this->_useInlineCss) { $html .= ' class="' . $cssClass . '"'; } else { $html .= ' style="' . $cssClass . '"'; } if ($colSpan > 1) { $html .= ' colspan="' . $colSpan . '"'; } if ($rowSpan > 1) { $html .= ' rowspan="' . $rowSpan . '"'; } $html .= '>'; // Image? $html .= $this->_writeImageTagInCell($pSheet, $cell->getCoordinate()); // Cell data if ($this->_useInlineCss) { $html .= '<span style="' . $cssClass . '">'; } $html .= $cellData; if ($this->_useInlineCss) { $html .= '</span>'; } // 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."); } }