PHPExcel_Writer
[ class tree: PHPExcel_Writer ] [ index: PHPExcel_Writer ] [ all elements ]

Source for file HTML.php

Documentation is available at HTML.php

  1. <?php
  2. /**
  3.  * PHPExcel
  4.  *
  5.  * Copyright (c) 2006 - 2009 PHPExcel
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  *
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  20.  *
  21.  * @category   PHPExcel
  22.  * @package    PHPExcel_Writer
  23.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  26.  */
  27.  
  28.  
  29. /** PHPExcel_IWriter */
  30. require_once 'PHPExcel/Writer/IWriter.php';
  31.  
  32. /** PHPExcel_Cell */
  33. require_once 'PHPExcel/Cell.php';
  34.  
  35. /** PHPExcel_RichText */
  36. require_once 'PHPExcel/RichText.php';
  37.  
  38. /** PHPExcel_Shared_Drawing */
  39. require_once 'PHPExcel/Shared/Drawing.php';
  40.  
  41. /** PHPExcel_Shared_String */
  42. require_once 'PHPExcel/Shared/String.php';
  43.  
  44. /** PHPExcel_HashTable */
  45. require_once 'PHPExcel/HashTable.php';
  46.  
  47.  
  48. /**
  49.  * PHPExcel_Writer_HTML
  50.  *
  51.  * @category   PHPExcel
  52.  * @package    PHPExcel_Writer
  53.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  54.  */
  55. class PHPExcel_Writer_HTML implements PHPExcel_Writer_IWriter {
  56.     /**
  57.      * PHPExcel object
  58.      *
  59.      * @var PHPExcel 
  60.      */
  61.     private $_phpExcel;
  62.  
  63.     /**
  64.      * Sheet index to write
  65.      *
  66.      * @var int 
  67.      */
  68.     private $_sheetIndex;
  69.  
  70.     /**
  71.      * Pre-calculate formulas
  72.      *
  73.      * @var boolean 
  74.      */
  75.     private $_preCalculateFormulas = true;
  76.  
  77.     /**
  78.      * Images root
  79.      *
  80.      * @var string 
  81.      */
  82.     private $_imagesRoot = '.';
  83.  
  84.     /**
  85.      * Create a new PHPExcel_Writer_HTML
  86.      *
  87.      * @param     PHPExcel    $phpExcel    PHPExcel object
  88.      */
  89.     public function __construct(PHPExcel $phpExcel{
  90.         $this->_phpExcel = $phpExcel;
  91.         $this->_sheetIndex = 0;
  92.         $this->_imagesRoot = '.';
  93.     }
  94.  
  95.     /**
  96.      * Save PHPExcel to file
  97.      *
  98.      * @param     string         $pFileName 
  99.      * @throws     Exception
  100.      */
  101.     public function save($pFilename null{
  102.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  103.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  104.  
  105.         // Open file
  106.         $fileHandle fopen($pFilename'w');
  107.         if ($fileHandle === false{
  108.             throw new Exception("Could not open file $pFilename for writing.");
  109.         }
  110.  
  111.         // Write headers
  112.         fwrite($fileHandle$this->generateHTMLHeader(true));
  113.  
  114.         // Write data
  115.         fwrite($fileHandle$this->generateSheetData());
  116.  
  117.         // Write footer
  118.         fwrite($fileHandle$this->generateHTMLFooter());
  119.  
  120.         // Close file
  121.         fclose($fileHandle);
  122.  
  123.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  124.     }
  125.  
  126.     /**
  127.      * Map VAlign
  128.      */
  129.     private function _mapVAlign($vAlign{
  130.         switch ($vAlign{
  131.             case PHPExcel_Style_Alignment::VERTICAL_BOTTOMreturn 'bottom';
  132.             case PHPExcel_Style_Alignment::VERTICAL_TOPreturn 'top';
  133.             case PHPExcel_Style_Alignment::VERTICAL_CENTER:
  134.             case PHPExcel_Style_Alignment::VERTICAL_JUSTIFYreturn 'middle';
  135.             defaultreturn ' baseline';
  136.         }
  137.     }
  138.  
  139.     /**
  140.      * Map HAlign
  141.      */
  142.     private function _mapHAlign($hAlign{
  143.         switch ($hAlign{
  144.             case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL:
  145.             case PHPExcel_Style_Alignment::HORIZONTAL_LEFTreturn 'left';
  146.             case PHPExcel_Style_Alignment::HORIZONTAL_RIGHTreturn 'right';
  147.             case PHPExcel_Style_Alignment::HORIZONTAL_CENTERreturn 'center';
  148.             case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFYreturn 'justify';
  149.             defaultreturn ' baseline';
  150.         }
  151.     }
  152.  
  153.     /**
  154.      * Map border style
  155.      */
  156.     private function _mapBorderStyle($borderStyle{
  157.         switch ($borderStyle{
  158.             case PHPExcel_Style_Border::BORDER_NONEreturn '0px';
  159.             case PHPExcel_Style_Border::BORDER_DASHEDreturn '1px dashed';
  160.             case PHPExcel_Style_Border::BORDER_DOTTEDreturn '1px dotted';
  161.             case PHPExcel_Style_Border::BORDER_THICKreturn '2px solid';
  162.             defaultreturn '1px solid'// map others to thin
  163.         }
  164.     }
  165.  
  166.     /**
  167.      * Get sheet index
  168.      *
  169.      * @return int 
  170.      */
  171.     public function getSheetIndex({
  172.         return $this->_sheetIndex;
  173.     }
  174.  
  175.     /**
  176.      * Set sheet index
  177.      *
  178.      * @param    int        $pValue        Sheet index
  179.      */
  180.     public function setSheetIndex($pValue 0{
  181.         $this->_sheetIndex = $pValue;
  182.     }
  183.  
  184.     /**
  185.      * Write all sheets (resets sheetIndex to NULL)
  186.      */
  187.     public function writeAllSheets({
  188.         $this->_sheetIndex = null;
  189.     }
  190.  
  191.     /**
  192.      * Generate HTML header
  193.      *
  194.      * @param    boolean        $pIncludeStyles        Include styles?
  195.      * @return    string 
  196.      * @throws Exception
  197.      */
  198.     public function generateHTMLHeader($pIncludeStyles false{
  199.         // PHPExcel object known?
  200.         if (is_null($this->_phpExcel)) {
  201.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  202.         }
  203.  
  204.         // Construct HTML
  205.         $html '';
  206.         $html .= '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' "\r\n";
  207.         $html .= '<!-- Generated by PHPExcel - http://www.phpexcel.net -->' "\r\n";
  208.         $html .= '<html>' "\r\n";
  209.         $html .= '  <head>' "\r\n";
  210.         $html .= '    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">' "\r\n";
  211.         $html .= '    <title>' htmlspecialchars($this->_phpExcel->getProperties()->getTitle()) '</title>' "\r\n";
  212.         if ($pIncludeStyles{
  213.             $html .= $this->generateStyles(true);
  214.         }
  215.         $html .= '  </head>' "\r\n";
  216.         $html .= '' "\r\n";
  217.         $html .= '  <body>' "\r\n";
  218.  
  219.         // Return
  220.         return $html;
  221.     }
  222.  
  223.     /**
  224.      * Generate sheet data
  225.      *
  226.      * @return    string 
  227.      * @throws Exception
  228.      */
  229.     public function generateSheetData({
  230.         // PHPExcel object known?
  231.         if (is_null($this->_phpExcel)) {
  232.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  233.         }
  234.  
  235.         // Fetch sheets
  236.         $sheets array();
  237.         if (is_null($this->_sheetIndex)) {
  238.             $sheets $this->_phpExcel->getAllSheets();
  239.         else {
  240.             $sheets[$this->_phpExcel->getSheet($this->_sheetIndex);
  241.         }
  242.  
  243.         // Construct HTML
  244.         $html '';
  245.  
  246.         // Loop all sheets
  247.         foreach ($sheets as $sheet{
  248.             // Calculate hash code
  249.             $hashCode $sheet->getHashCode();
  250.  
  251.             // Get cell collection
  252.             $cellCollection $sheet->getCellCollection();
  253.  
  254.             // Write table header
  255.             $html .= $this->_generateTableHeader($hashCode);
  256.  
  257.             // Get worksheet dimension
  258.             $dimension explode(':'$sheet->calculateWorksheetDimension());
  259.             $dimension[0PHPExcel_Cell::coordinateFromString($dimension[0]);
  260.             $dimension[0][0PHPExcel_Cell::columnIndexFromString($dimension[0][0]1;
  261.             $dimension[1PHPExcel_Cell::coordinateFromString($dimension[1]);
  262.             $dimension[1][0PHPExcel_Cell::columnIndexFromString($dimension[1][0]1;
  263.  
  264.             // Loop trough cells
  265.             $rowData null;
  266.             for ($row $dimension[0][1]$row <= $dimension[1][1]++$row{
  267.                 // Start a new row
  268.                 $rowData array();
  269.  
  270.                 // Loop trough columns
  271.                 for ($column $dimension[0][0]$column <= $dimension[1][0]++$column{
  272.                     // Cell exists?
  273.                     if ($sheet->cellExistsByColumnAndRow($column$row)) {
  274.                         $rowData[$column$sheet->getCellByColumnAndRow($column$row);
  275.                     else {
  276.                         $rowData[$column'';
  277.                     }
  278.                 }
  279.  
  280.                 // Write row
  281.                 $html .= $this->_generateRow($sheet$rowData$row 1);
  282.             }
  283.  
  284.             // Write table footer
  285.             $html .= $this->_generateTableFooter();
  286.         }
  287.  
  288.         // Return
  289.         return $html;
  290.     }
  291.  
  292.     /**
  293.      * Generate image tag in cell
  294.      *
  295.      * @param    PHPExcel_Worksheet     $pSheet            PHPExcel_Worksheet
  296.      * @param    string                $coordinates    Cell coordinates
  297.      * @return    string 
  298.      * @throws    Exception
  299.      */
  300.     private function _writeImageTagInCell(PHPExcel_Worksheet $pSheet$coordinates{
  301.         // Construct HTML
  302.         $html '';
  303.  
  304.         // Write images
  305.         foreach ($pSheet->getDrawingCollection(as $drawing{
  306.             if ($drawing instanceof PHPExcel_Worksheet_BaseDrawing{
  307.                 if ($drawing->getCoordinates(== $coordinates{
  308.                     $filename $drawing->getPath();
  309.  
  310.                     // Strip off eventual '.'
  311.                     if (substr($filename01== '.'{
  312.                         $filename substr($filename1);
  313.                     }
  314.  
  315.                     // Prepend images root
  316.                     $filename $this->getImagesRoot($filename;
  317.  
  318.                     // Strip off eventual '.'
  319.                     if (substr($filename01== '.' && substr($filename02!= './'{
  320.                         $filename substr($filename1);
  321.                     }
  322.  
  323.                     // Convert UTF8 data to PCDATA
  324.                     $filename htmlspecialchars($filename);
  325.  
  326.                     $html .= "\r\n";
  327.                     $html .= '        <img  style="position: relative; left: ' $drawing->getOffsetX('px; top: ' $drawing->getOffsetY('px; width: ' $drawing->getWidth('px; height: ' $drawing->getHeight('px;" src="' $filename '" border="0">' "\r\n";
  328.                 }
  329.             }
  330.         }
  331.  
  332.         // Return
  333.         return $html;
  334.     }
  335.  
  336.     /**
  337.      * Generate CSS styles
  338.      *
  339.      * @param    boolean    $generateSurroundingHTML    Generate surrounding HTML tags? (<style> and </style>)
  340.      * @return    string 
  341.      * @throws    Exception
  342.      */
  343.     public function generateStyles($generateSurroundingHTML true{
  344.         // PHPExcel object known?
  345.         if (is_null($this->_phpExcel)) {
  346.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  347.         }
  348.  
  349.         // Construct HTML
  350.         $html '';
  351.  
  352.         // Start styles
  353.         if ($generateSurroundingHTML{
  354.             $html .= '    <style type="text/css">' "\r\n";
  355.             $html .= '      html {' "\r\n";
  356.             $html .= '        font-family: Calibri, Arial, Helvetica, sans-serif;' "\r\n";
  357.             $html .= '        font-size: 10pt;' "\r\n";
  358.             $html .= '        background-color: white;' "\r\n";
  359.             $html .= '      }' "\r\n";
  360.         }
  361.  
  362.         // Fetch sheets
  363.         $sheets array();
  364.         if (is_null($this->_sheetIndex)) {
  365.             $sheets $this->_phpExcel->getAllSheets();
  366.         else {
  367.             $sheets[$this->_phpExcel->getSheet($this->_sheetIndex);
  368.         }
  369.  
  370.         // Write styles per sheet
  371.         foreach ($sheets as $sheet{
  372.             // Calculate hash code
  373.             $hashCode $sheet->getHashCode();
  374.  
  375.             // Write styles
  376.             $html .= '      table.sheet' $hashCode ', table.sheet' $hashCode ' td {' "\r\n";
  377.             if ($sheet->getShowGridlines()) {
  378.                 $html .= '        border: 1px dotted black;' "\r\n";
  379.             }
  380.             $html .= '        page-break-after: always;' "\r\n";
  381.             $html .= '      }' "\r\n";
  382.  
  383.             // Default column width
  384.             $columnDimension $sheet->getDefaultColumnDimension();
  385.  
  386.             $html .= '      table.sheet' $hashCode ' td {' "\r\n";
  387.             $html .= '        width: ' PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) 'px;' "\r\n";
  388.             if ($columnDimension->getVisible(=== false{
  389.                 $html .= '        display: none;' "\r\n";
  390.                 $html .= '        visibility: hidden;' "\r\n";
  391.             }
  392.             $html .= '      }' "\r\n";
  393.  
  394.             // Calculate column widths
  395.             $sheet->calculateColumnWidths();
  396.             foreach ($sheet->getColumnDimensions(as $columnDimension{
  397.                 $column PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) 1;
  398.  
  399.                 $html .= '      table.sheet' $hashCode ' td.column' $column  ' {' "\r\n";
  400.                 $html .= '        width: ' PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) 'px;' "\r\n";
  401.                 if ($columnDimension->getVisible(=== false{
  402.                     $html .= '        display: none;' "\r\n";
  403.                     $html .= '        visibility: hidden;' "\r\n";
  404.                 }
  405.                 $html .= '      }' "\r\n";
  406.             }
  407.  
  408.             // Default row height
  409.             $rowDimension $sheet->getDefaultRowDimension();
  410.  
  411.             $html .= '      table.sheet' $hashCode ' tr {' "\r\n";
  412.             // height is disproportionately large
  413.             $px_height roundPHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) 12 );
  414.             $html .= '        height: ' $px_height 'px;' "\r\n";
  415.             if ($rowDimension->getVisible(=== false{
  416.                 $html .= '        display: none;' "\r\n";
  417.                 $html .= '        visibility: hidden;' "\r\n";
  418.             }
  419.             $html .= '      }' "\r\n";
  420.  
  421.             // Calculate row heights
  422.             foreach ($sheet->getRowDimensions(as $rowDimension{
  423.                 $html .= '      table.sheet' $hashCode ' tr.row' ($rowDimension->getRowIndex(1)  ' {' "\r\n";
  424.                 // height is disproportionately large
  425.                 $px_height roundPHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) 12 );
  426.                 $html .= '        height: ' $px_height 'px;' "\r\n";
  427.                 if ($rowDimension->getVisible(=== false{
  428.                     $html .= '        display: none;' "\r\n";
  429.                     $html .= '        visibility: hidden;' "\r\n";
  430.                 }
  431.                 $html .= '      }' "\r\n";
  432.             }
  433.  
  434.             // Calculate cell style hashes
  435.             $cellStyleHashes new PHPExcel_HashTable();
  436.             $aStyles $sheet->getStyles();
  437.             $cellStyleHashes->addFromSource$aStyles );
  438.             $addedStyles array();
  439.             foreach ($aStyles as $style{
  440.                 if(isset($addedStyles[$style->getHashIndex()])) continue;
  441.                 $html .= $this->_createCSSStyle$style );
  442.                 $addedStyles[$style->getHashIndex()true;
  443.             }
  444.         }
  445.  
  446.         // End styles
  447.         if ($generateSurroundingHTML{
  448.             $html .= '    </style>' "\r\n";
  449.         }
  450.  
  451.         // Return
  452.         return $html;
  453.     }
  454.  
  455.     /**
  456.      * Create CSS style
  457.      *
  458.      * @param    PHPExcel_Style         $pStyle            PHPExcel_Style
  459.      * @return    string 
  460.      */
  461.     private function _createCSSStyle(PHPExcel_Style $pStyle{
  462.         // Construct HTML
  463.         $html '';
  464.  
  465.         // Create CSS
  466.         $html .= '      .style' $pStyle->getHashIndex(' {' "\r\n";
  467.         $html .= $this->_createCSSStyleAlignment($pStyle->getAlignment());
  468.         $html .= $this->_createCSSStyleFont($pStyle->getFont());
  469.         $html .= $this->_createCSSStyleBorders($pStyle->getBorders());
  470.         $html .= $this->_createCSSStyleFill($pStyle->getFill());
  471.         $html .= '      }' "\r\n";
  472.  
  473.         // Return
  474.         return $html;
  475.     }
  476.  
  477.     /**
  478.      * Create CSS style (PHPExcel_Style_Alignment)
  479.      *
  480.      * @param    PHPExcel_Style_Alignment         $pStyle            PHPExcel_Style_Alignment
  481.      * @return    string 
  482.      */
  483.     private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle{
  484.         // Construct HTML
  485.         $html '';
  486.  
  487.         // Create CSS
  488.         $html .= '        vertical-align: '     $this->_mapVAlign($pStyle->getVertical()) ';' "\r\n";
  489.         $html .= '        text-align: '         $this->_mapHAlign($pStyle->getHorizontal()) ';' "\r\n";
  490.  
  491.         // Return
  492.         return $html;
  493.     }
  494.  
  495.     /**
  496.      * Create CSS style (PHPExcel_Style_Font)
  497.      *
  498.      * @param    PHPExcel_Style_Font         $pStyle            PHPExcel_Style_Font
  499.      * @return    string 
  500.      */
  501.     private function _createCSSStyleFont(PHPExcel_Style_Font $pStyle{
  502.         // Construct HTML
  503.         $html '';
  504.  
  505.         // Create CSS
  506.         if ($pStyle->getBold()) {
  507.             $html .= '        font-weight: bold;' "\r\n";
  508.         }
  509.         if ($pStyle->getUnderline(!= PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStriketrough()) {
  510.             $html .= '        text-decoration: underline line-through;' "\r\n";
  511.         else if ($pStyle->getUnderline(!= PHPExcel_Style_Font::UNDERLINE_NONE{
  512.             $html .= '        text-decoration: underline;' "\r\n";
  513.         else if ($pStyle->getStriketrough()) {
  514.             $html .= '        text-decoration: line-through;' "\r\n";
  515.         }
  516.         if ($pStyle->getItalic()) {
  517.             $html .= '        font-style: italic;' "\r\n";
  518.         }
  519.  
  520.         $html .= '        color: '                 '#' $pStyle->getColor()->getRGB(';' "\r\n";
  521.         $html .= '        font-family: '         '\'' $pStyle->getName('\';' "\r\n";
  522.         $html .= '        font-size: '             $pStyle->getSize('pt;' "\r\n";
  523.  
  524.         // Return
  525.         return $html;
  526.     }
  527.  
  528.     /**
  529.      * Create CSS style (PHPExcel_Style_Borders)
  530.      *
  531.      * @param    PHPExcel_Style_Borders         $pStyle            PHPExcel_Style_Borders
  532.      * @return    string 
  533.      */
  534.     private function _createCSSStyleBorders(PHPExcel_Style_Borders $pStyle{
  535.         // Construct HTML
  536.         $html '';
  537.  
  538.         // Create CSS
  539.         $html .= '        border-bottom: '         $this->_createCSSStyleBorder($pStyle->getBottom()) ';' "\r\n";
  540.         $html .= '        border-top: '         $this->_createCSSStyleBorder($pStyle->getTop()) ';' "\r\n";
  541.         $html .= '        border-left: '         $this->_createCSSStyleBorder($pStyle->getLeft()) ';' "\r\n";
  542.         $html .= '        border-right: '         $this->_createCSSStyleBorder($pStyle->getRight()) ';' "\r\n";
  543.  
  544.         // Return
  545.         return $html;
  546.     }
  547.  
  548.     /**
  549.      * Create CSS style (PHPExcel_Style_Border)
  550.      *
  551.      * @param    PHPExcel_Style_Border        $pStyle            PHPExcel_Style_Border
  552.      * @return    string 
  553.      */
  554.     private function _createCSSStyleBorder(PHPExcel_Style_Border $pStyle{
  555.         // Construct HTML
  556.         $html '';
  557.  
  558.         // Create CSS
  559.         $html .= $this->_mapBorderStyle($pStyle->getBorderStyle()) ' #' $pStyle->getColor()->getRGB();
  560.  
  561.         // Return
  562.         return $html;
  563.     }
  564.  
  565.     /**
  566.      * Create CSS style (PHPExcel_Style_Fill)
  567.      *
  568.      * @param    PHPExcel_Style_Fill        $pStyle            PHPExcel_Style_Fill
  569.      * @return    string 
  570.      */
  571.     private function _createCSSStyleFill(PHPExcel_Style_Fill $pStyle{
  572.         // Construct HTML
  573.         $html '';
  574.  
  575.         // Create CSS
  576.         $html .= '        background-color: '     '#' $pStyle->getStartColor()->getRGB(';' "\r\n";
  577.  
  578.         // Return
  579.         return $html;
  580.     }
  581.  
  582.     /**
  583.      * Generate HTML footer
  584.      */
  585.     public function generateHTMLFooter({
  586.         // Construct HTML
  587.         $html '';
  588.         $html .= '  </body>' "\r\n";
  589.         $html .= '</html>' "\r\n";
  590.  
  591.         // Return
  592.         return $html;
  593.     }
  594.  
  595.     /**
  596.      * Generate table header
  597.      *
  598.      * @param     string    $pIdentifier    Identifier for the table
  599.      * @return    string 
  600.      * @throws    Exception
  601.      */
  602.     private function _generateTableHeader($pIdentifier ''{
  603.         // Construct HTML
  604.         $html '';
  605.         $html .= '    <table border="0" cellpadding="0" cellspacing="0" class="sheet' $pIdentifier '">' "\r\n";
  606.  
  607.         // Return
  608.         return $html;
  609.     }
  610.  
  611.     /**
  612.      * Generate table footer
  613.      *
  614.      * @throws    Exception
  615.      */
  616.     private function _generateTableFooter({
  617.         // Construct HTML
  618.         $html '';
  619.         $html .= '    </table>' "\r\n";
  620.  
  621.         // Return
  622.         return $html;
  623.     }
  624.  
  625.     /**
  626.      * Generate row
  627.      *
  628.      * @param    PHPExcel_Worksheet     $pSheet            PHPExcel_Worksheet
  629.      * @param    array                $pValues        Array containing cells in a row
  630.      * @param    int                    $pRow            Row number
  631.      * @return    string 
  632.      * @throws    Exception
  633.      */
  634.     private function _generateRow(PHPExcel_Worksheet $pSheet$pValues null$pRow 0{
  635.         if (is_array($pValues)) {
  636.             // Construct HTML
  637.             $html '';
  638.  
  639.             // Write row start
  640.             $html .= '        <tr class="row' $pRow '">' "\r\n";
  641.  
  642.             // Write cells
  643.             $colNum 0;
  644.             foreach ($pValues as $cell{
  645.                 $cellData '&nbsp;';
  646.                 $cssClass 'column' $colNum;
  647.                 $colSpan 1;
  648.                 $rowSpan 1;
  649.                 $writeCell true;    // Write cell
  650.  
  651.                 // PHPExcel_Cell
  652.                 if ($cell instanceof PHPExcel_Cell{
  653.                     // Value
  654.                     if ($cell->getValue(instanceof PHPExcel_RichText{
  655.                         // Loop trough rich text elements
  656.                         $elements $cell->getValue()->getRichTextElements();
  657.                         foreach ($elements as $element{
  658.                             // Rich text start?
  659.                             if ($element instanceof PHPExcel_RichText_Run{
  660.                                 $cellData .= '<span style="' .
  661.                                     str_replace("\r\n"'',
  662.                                         $this->_createCSSStyleFont($element->getFont())
  663.                                     '">';
  664.  
  665.                                 if ($element->getFont()->getSuperScript()) {
  666.                                     $cellData .= '<sup>';
  667.                                 else if ($element->getFont()->getSubScript()) {
  668.                                     $cellData .= '<sub>';
  669.                                 }
  670.                             }
  671.  
  672.                             // Convert UTF8 data to PCDATA
  673.                             $cellText $element->getText();
  674.                             $cellData .= htmlspecialchars($cellText);
  675.  
  676.                             if ($element instanceof PHPExcel_RichText_Run{
  677.                                 if ($element->getFont()->getSuperScript()) {
  678.                                     $cellData .= '</sup>';
  679.                                 else if ($element->getFont()->getSubScript()) {
  680.                                     $cellData .= '</sub>';
  681.                                 }
  682.  
  683.                                 $cellData .= '</span>';
  684.                             }
  685.                         }
  686.                     else {
  687.                         if ($this->_preCalculateFormulas{
  688.                             $cellData PHPExcel_Style_NumberFormat::toFormattedString(
  689.                                 $cell->getCalculatedValue(),
  690.                                 $pSheet->getstyle$cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  691.                             );
  692.                         else {
  693.                             $cellData PHPExcel_Style_NumberFormat::ToFormattedString(
  694.                                 $cell->getValue(),
  695.                                 $pSheet->getstyle$cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  696.                             );
  697.                         }
  698.  
  699.                         // Convert UTF8 data to PCDATA
  700.                         $cellData htmlspecialchars($cellData);
  701.                     }
  702.  
  703.                     // Check value
  704.                     if ($cellData == ''{
  705.                         $cellData '&nbsp;';
  706.                     }
  707.  
  708.                     // Extend CSS class?
  709.                     if (array_key_exists($cell->getCoordinate()$pSheet->getStyles())) {
  710.                         $cssClass .= ' style' $pSheet->getStyle($cell->getCoordinate())->getHashIndex();
  711.                     }
  712.                 else {
  713.                     $cell new PHPExcel_Cell(
  714.                         PHPExcel_Cell::stringFromColumnIndex($colNum),
  715.                         ($pRow 1),
  716.                         '',
  717.                         null,
  718.                         null
  719.                     );
  720.                 }
  721.  
  722.                 // Hyperlink?
  723.                 if ($cell->hasHyperlink(&& !$cell->getHyperlink()->isInternal()) {
  724.                     $cellData '<a href="' htmlspecialchars($cell->getHyperlink()->getUrl()) '" title="' htmlspecialchars($cell->getHyperlink()->getTooltip()) '">' $cellData '</a>';
  725.                 }
  726.  
  727.                 // Column/rowspan
  728.                 foreach ($pSheet->getMergeCells(as $cells{
  729.                     if ($cell->isInRange($cells)) {
  730.                         list($firstPHPExcel_Cell::splitRange($cells);
  731.  
  732.                         if ($first == $cell->getCoordinate()) {
  733.                             list($colSpan$rowSpanPHPExcel_Cell::rangeDimension($cells);
  734.                         else {
  735.                             $writeCell false;
  736.                         }
  737.  
  738.                         break;
  739.                     }
  740.                 }
  741.  
  742.                 // Write
  743.                 if ($writeCell{
  744.                     // Column start
  745.                     $html .= '          <td';
  746.                         $html .= ' class="' $cssClass '"';
  747.                         if ($colSpan 1{
  748.                             $html .= ' colspan="' $colSpan '"';
  749.                         }
  750.                         if ($rowSpan 1{
  751.                             $html .= ' rowspan="' $rowSpan '"';
  752.                         }
  753.                     $html .= '>';
  754.  
  755.                     // Image?
  756.                     $html .= $this->_writeImageTagInCell($pSheet$cell->getCoordinate());
  757.  
  758.                     // Cell data
  759.                     $html .= $cellData;
  760.  
  761.                     // Column end
  762.                     $html .= '</td>' "\r\n";
  763.                 }
  764.  
  765.                 // Next column
  766.                 ++$colNum;
  767.             }
  768.  
  769.             // Write row end
  770.             $html .= '        </tr>' "\r\n";
  771.  
  772.             // Return
  773.             return $html;
  774.         else {
  775.             throw new Exception("Invalid parameters passed.");
  776.         }
  777.     }
  778.  
  779.  
  780.     /**
  781.      * Get Pre-Calculate Formulas
  782.      *
  783.      * @return boolean 
  784.      */
  785.     public function getPreCalculateFormulas({
  786.         return $this->_preCalculateFormulas;
  787.     }
  788.  
  789.     /**
  790.      * Set Pre-Calculate Formulas
  791.      *
  792.      * @param boolean $pValue    Pre-Calculate Formulas?
  793.      */
  794.     public function setPreCalculateFormulas($pValue true{
  795.         $this->_preCalculateFormulas = $pValue;
  796.     }
  797.  
  798.     /**
  799.      * Get images root
  800.      *
  801.      * @return string 
  802.      */
  803.     public function getImagesRoot({
  804.         return $this->_imagesRoot;
  805.     }
  806.  
  807.     /**
  808.      * Set images root
  809.      *
  810.      * @param string $pValue 
  811.      */
  812.     public function setImagesRoot($pValue '.'{
  813.         $this->_imagesRoot = $pValue;
  814.     }
  815. }

Documentation generated on Mon, 05 Jan 2009 20:37:58 +0100 by phpDocumentor 1.4.1