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 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.3, 2008-08-25
  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 - 2008 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.         // Open file
  103.         $fileHandle = fopen($pFilename, 'w');
  104.         if ($fileHandle === false) {
  105.             throw new Exception("Could not open file $pFilename for writing.");
  106.         }
  107.         
  108.         // Write headers
  109.         fwrite($fileHandle, $this->generateHTMLHeader());
  110.         fwrite($fileHandle, $this->generateStyles(true));
  111.         
  112.         // Write data
  113.         fwrite($fileHandle, $this->generateSheetData());
  114.         
  115.         // Write footer
  116.         fwrite($fileHandle, $this->generateHTMLFooter());
  117.         
  118.         // Close file
  119.         fclose($fileHandle);
  120.     }
  121.      
  122.     /**
  123.      * Map VAlign
  124.      */    
  125.     private function _mapVAlign($vAlign) {
  126.         switch ($vAlign) {
  127.             case PHPExcel_Style_Alignment::VERTICAL_BOTTOM: return 'bottom';
  128.             case PHPExcel_Style_Alignment::VERTICAL_TOP: return 'top';
  129.             case PHPExcel_Style_Alignment::VERTICAL_CENTER:
  130.             case PHPExcel_Style_Alignment::VERTICAL_JUSTIFY: return 'middle';
  131.             default: return ' baseline';
  132.         }
  133.     }
  134.     
  135.     /**
  136.      * Map HAlign
  137.      */    
  138.     private function _mapHAlign($hAlign) {
  139.         switch ($hAlign) {
  140.             case PHPExcel_Style_Alignment::HORIZONTAL_GENERAL:
  141.             case PHPExcel_Style_Alignment::HORIZONTAL_LEFT: return 'left';
  142.             case PHPExcel_Style_Alignment::HORIZONTAL_RIGHT: return 'right';
  143.             case PHPExcel_Style_Alignment::HORIZONTAL_CENTER: return 'center';
  144.             case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY: return 'justify';
  145.             default: return ' baseline';
  146.         }
  147.     }
  148.     
  149.     /**
  150.      * Map border style
  151.      */    
  152.     private function _mapBorderStyle($borderStyle) {
  153.         switch ($borderStyle) {
  154.             case PHPExcel_Style_Border::BORDER_NONE: return '0px';
  155.             case PHPExcel_Style_Border::BORDER_DASHED: return '1px dashed';
  156.             case PHPExcel_Style_Border::BORDER_DOTTED: return '1px dotted';
  157.             case PHPExcel_Style_Border::BORDER_THICK: return '2px solid';
  158.             default: return '1px solid'; // map others to thin
  159.         }
  160.     }
  161.     
  162.     /**
  163.      * Get sheet index
  164.      * 
  165.      * @return int 
  166.      */
  167.     public function getSheetIndex() {
  168.         return $this->_sheetIndex;
  169.     }
  170.     
  171.     /**
  172.      * Set sheet index
  173.      * 
  174.      * @param    int        $pValue        Sheet index
  175.      */
  176.     public function setSheetIndex($pValue = 0) {
  177.         $this->_sheetIndex = $pValue;
  178.     }
  179.     
  180.     /**
  181.      * Write all sheets (resets sheetIndex to NULL)
  182.      */
  183.     public function writeAllSheets() {
  184.         $this->_sheetIndex = null;
  185.     }
  186.     
  187.     /**
  188.      * Generate HTML header
  189.      * 
  190.      * @return    string 
  191.      * @throws Exception
  192.      */
  193.     public function generateHTMLHeader() {
  194.         // PHPExcel object known?
  195.         if (is_null($this->_phpExcel)) {
  196.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  197.         }
  198.         
  199.         // Construct HTML
  200.         $html = '';
  201.         $html .= '<!-- Generated by PHPExcel - http://www.phpexcel.net -->' . "\r\n";
  202.         $html .= '<html>' . "\r\n";
  203.         $html .= '  <head>' . "\r\n";
  204.         $html .= '    <title>' . $this->_phpExcel->getProperties()->getTitle() . '</title>' . "\r\n";
  205.         $html .= '  </head>' . "\r\n";
  206.         $html .= '' . "\r\n";
  207.         $html .= '  <body>' . "\r\n";
  208.  
  209.         // Return
  210.         return $html;
  211.     }
  212.     
  213.     /**
  214.      * Generate sheet data
  215.      * 
  216.      * @return    string 
  217.      * @throws Exception
  218.      */
  219.     public function generateSheetData() {
  220.         // PHPExcel object known?
  221.         if (is_null($this->_phpExcel)) {
  222.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  223.         }
  224.         
  225.         // Fetch sheets
  226.         $sheets = array();
  227.         if (is_null($this->_sheetIndex)) {
  228.             $sheets = $this->_phpExcel->getAllSheets();
  229.         } else {
  230.             $sheets[] = $this->_phpExcel->getSheet($this->_sheetIndex);
  231.         }
  232.         
  233.         // Construct HTML
  234.         $html = '';
  235.         
  236.         // Loop all sheets
  237.         foreach ($sheets as $sheet) {
  238.             // Calculate hash code
  239.             $hashCode = $sheet->getHashCode();
  240.                 
  241.             // Get cell collection
  242.             $cellCollection = $sheet->getCellCollection();
  243.             
  244.             // Write table header
  245.             $html .= $this->_generateTableHeader($hashCode);
  246.             
  247.             // Get worksheet dimension
  248.             $dimension = explode(':', $sheet->calculateWorksheetDimension());
  249.             $dimension[0] = PHPExcel_Cell::coordinateFromString($dimension[0]);
  250.             $dimension[0][0] = PHPExcel_Cell::columnIndexFromString($dimension[0][0]) - 1;
  251.             $dimension[1] = PHPExcel_Cell::coordinateFromString($dimension[1]);
  252.             $dimension[1][0] = PHPExcel_Cell::columnIndexFromString($dimension[1][0]) - 1;
  253.     
  254.             // Loop trough cells
  255.             $rowData = null;
  256.             for ($row = $dimension[0][1]; $row <= $dimension[1][1]; $row++) {
  257.                 // Start a new row
  258.                 $rowData = array();
  259.                             
  260.                 // Loop trough columns
  261.                 for ($column = $dimension[0][0]; $column <= $dimension[1][0]; $column++) {
  262.                     // Cell exists?
  263.                     if ($sheet->cellExistsByColumnAndRow($column, $row)) {
  264.                         $rowData[$column] = $sheet->getCellByColumnAndRow($column, $row);
  265.                     } else {
  266.                         $rowData[$column] = '';
  267.                     }
  268.                 }
  269.     
  270.                 // Write row
  271.                 $html .= $this->_generateRow($sheet, $rowData, $row - 1);
  272.             }
  273.                 
  274.             // Write table footer
  275.             $html .= $this->_generateTableFooter();
  276.         }
  277.         
  278.         // Return
  279.         return $html;
  280.     }
  281.  
  282.     /**
  283.      * Generate image tag in cell
  284.      * 
  285.      * @param    PHPExcel_Worksheet     $pSheet            PHPExcel_Worksheet
  286.      * @param    string                $coordinates    Cell coordinates
  287.      * @retrun    string
  288.      * @throws    Exception
  289.      */
  290.     private function _writeImageTagInCell(PHPExcel_Worksheet $pSheet, $coordinates) {
  291.         // Construct HTML
  292.         $html = '';
  293.  
  294.         // Write images
  295.         foreach ($pSheet->getDrawingCollection() as $drawing) {
  296.             if ($drawing instanceof PHPExcel_Worksheet_BaseDrawing) {
  297.                 if ($drawing->getCoordinates() == $coordinates) {
  298.                     $filename = $drawing->getPath();
  299.                     
  300.                     // Strip off eventual '.'
  301.                     if (substr($filename, 0, 1) == '.') {
  302.                         $filename = substr($filename, 1);
  303.                     }
  304.                     
  305.                     // Prepend images root
  306.                     $filename = $this->getImagesRoot() . $filename;
  307.  
  308.                     $html .= "\r\n";
  309.                     $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";
  310.                 }
  311.             }
  312.         }
  313.  
  314.         // Return
  315.         return $html;
  316.     }
  317.     
  318.     /**
  319.      * Generate CSS styles
  320.      * 
  321.      * @param    boolean    $generateSurroundingHTML    Generate surrounding HTML tags? (<style> and </style>)
  322.      * @return    string 
  323.      * @throws    Exception
  324.      */
  325.     public function generateStyles($generateSurroundingHTML = true) {
  326.         // PHPExcel object known?
  327.         if (is_null($this->_phpExcel)) {
  328.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  329.         }
  330.         
  331.         // Construct HTML
  332.         $html = '';
  333.             
  334.         // Start styles
  335.         if ($generateSurroundingHTML) {
  336.             $html .= '    <style>' . "\r\n";
  337.             $html .= '    <!--' . "\r\n";
  338.             $html .= '      html {' . "\r\n";
  339.             $html .= '        font-family: Calibri, Arial, Helvetica, Sans Serif;' . "\r\n";
  340.             $html .= '        font-size: 10pt;' . "\r\n";
  341.             $html .= '        background-color: white;' . "\r\n";
  342.             $html .= '      }' . "\r\n";
  343.         }
  344.         
  345.         // Write styles per sheet
  346.         foreach ($this->_phpExcel->getAllSheets() as $sheet) {
  347.             // Calculate hash code
  348.             $hashCode = $sheet->getHashCode();
  349.                 
  350.             // Write styles
  351.             $html .= '      table.sheet' . $hashCode . ', table.sheet' . $hashCode . ' td {' . "\r\n";
  352.             if ($sheet->getShowGridlines()) {
  353.                 $html .= '        border: 1px dotted black;' . "\r\n";
  354.             }
  355.             $html .= '        page-break-after: always;' . "\r\n";
  356.             $html .= '      }' . "\r\n";                
  357.             
  358.             // Default column width
  359.             $columnDimension = $sheet->getDefaultColumnDimension();
  360.                 
  361.             $html .= '      table.sheet' . $hashCode . ' td {' . "\r\n";
  362.             $html .= '        width: ' . PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) . 'px;' . "\r\n";
  363.             if ($columnDimension->getVisible() === false) {
  364.                 $html .= '        display: none;' . "\r\n";
  365.                 $html .= '        visibility: hidden;' . "\r\n";
  366.             }
  367.             $html .= '      }' . "\r\n";
  368.                     
  369.             // Calculate column widths
  370.             $sheet->calculateColumnWidths();
  371.             foreach ($sheet->getColumnDimensions() as $columnDimension) {
  372.                 $column = PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) - 1;
  373.                 
  374.                 $html .= '      table.sheet' . $hashCode . ' td.column' . $column  . ' {' . "\r\n";
  375.                 $html .= '        width: ' . PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth()) . 'px;' . "\r\n";
  376.                 if ($columnDimension->getVisible() === false) {
  377.                     $html .= '        display: none;' . "\r\n";
  378.                     $html .= '        visibility: hidden;' . "\r\n";
  379.                 }
  380.                 $html .= '      }' . "\r\n";
  381.             }
  382.             
  383.             // Default row height
  384.             $rowDimension = $sheet->getDefaultRowDimension();
  385.             
  386.             $html .= '      table.sheet' . $hashCode . ' tr {' . "\r\n";
  387.             // height is disproportionately large
  388.             $px_height = round( PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) / 12 );
  389.             $html .= '        height: ' . $px_height . 'px;' . "\r\n";
  390.             if ($rowDimension->getVisible() === false) {
  391.                 $html .= '        display: none;' . "\r\n";
  392.                 $html .= '        visibility: hidden;' . "\r\n";
  393.             }
  394.             $html .= '      }' . "\r\n";
  395.             
  396.             // Calculate row heights
  397.             foreach ($sheet->getRowDimensions() as $rowDimension) {
  398.                 $html .= '      table.sheet' . $hashCode . ' tr.row' . ($rowDimension->getRowIndex() - 1)  . ' {' . "\r\n";
  399.                 // height is disproportionately large
  400.                 $px_height = round( PHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) / 12 );
  401.                 $html .= '        height: ' . $px_height . 'px;' . "\r\n";
  402.                 if ($rowDimension->getVisible() === false) {
  403.                     $html .= '        display: none;' . "\r\n";
  404.                     $html .= '        visibility: hidden;' . "\r\n";
  405.                 }
  406.                 $html .= '      }' . "\r\n";
  407.             }
  408.         
  409.             // Calculate cell style hashes
  410.             $cellStyleHashes = new PHPExcel_HashTable();
  411.             $cellStyleHashes->addFromSource( $sheet->getStyles() );
  412.             for ($i = 0; $i < $cellStyleHashes->count(); $i++) {
  413.                 $html .= $this->_createCSSStyle( $cellStyleHashes->getByIndex($i) );
  414.             }
  415.         }
  416.         
  417.         // End styles
  418.         if ($generateSurroundingHTML) {
  419.             $html .= '    -->' . "\r\n";
  420.             $html .= '    </style>' . "\r\n";
  421.         }
  422.  
  423.         // Return
  424.         return $html;
  425.     }
  426.     
  427.     /**
  428.      * Create CSS style
  429.      * 
  430.      * @param    PHPExcel_Style         $pStyle            PHPExcel_Style
  431.      * @return    string 
  432.      */
  433.     private function _createCSSStyle(PHPExcel_Style $pStyle) {
  434.         // Construct HTML
  435.         $html = '';
  436.             
  437.         // Create CSS
  438.         $html .= '      .style' . $pStyle->getHashCode() . ' {' . "\r\n";
  439.         $html .= $this->_createCSSStyleAlignment($pStyle->getAlignment());
  440.         $html .= $this->_createCSSStyleFont($pStyle->getFont());
  441.         $html .= $this->_createCSSStyleBorders($pStyle->getBorders());
  442.         $html .= $this->_createCSSStyleFill($pStyle->getFill());
  443.         $html .= '      }' . "\r\n";
  444.  
  445.         // Return
  446.         return $html;
  447.     }
  448.     
  449.     /**
  450.      * Create CSS style (PHPExcel_Style_Alignment)
  451.      * 
  452.      * @param    PHPExcel_Style_Alignment         $pStyle            PHPExcel_Style_Alignment
  453.      * @return    string 
  454.      */
  455.     private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle) {
  456.         // Construct HTML
  457.         $html = '';
  458.             
  459.         // Create CSS
  460.         $html .= '        vertical-align: '     . $this->_mapVAlign($pStyle->getVertical()) . ';' . "\r\n";
  461.         $html .= '        text-align: '         . $this->_mapHAlign($pStyle->getHorizontal()) . ';' . "\r\n";
  462.         
  463.         // Return
  464.         return $html;
  465.     }
  466.     
  467.     /**
  468.      * Create CSS style (PHPExcel_Style_Font)
  469.      * 
  470.      * @param    PHPExcel_Style_Font         $pStyle            PHPExcel_Style_Font
  471.      * @return    string 
  472.      */
  473.     private function _createCSSStyleFont(PHPExcel_Style_Font $pStyle) {
  474.         // Construct HTML
  475.         $html = '';
  476.             
  477.         // Create CSS
  478.         if ($pStyle->getBold()) {
  479.             $html .= '        font-weight: bold;' . "\r\n";
  480.         }
  481.         if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStriketrough()) {
  482.             $html .= '        text-decoration: underline line-through;' . "\r\n";
  483.         } else if ($pStyle->getUnderline() != PHPExcel_Style_Font::UNDERLINE_NONE) {
  484.             $html .= '        text-decoration: underline;' . "\r\n";
  485.         } else if ($pStyle->getStriketrough()) {
  486.             $html .= '        text-decoration: line-through;' . "\r\n";
  487.         }
  488.         if ($pStyle->getItalic()) {
  489.             $html .= '        font-style: italic;' . "\r\n";
  490.         }
  491.                 
  492.         $html .= '        color: '                 . '#' . $pStyle->getColor()->getRGB() . ';' . "\r\n";
  493.         $html .= '        font-family: '         . $pStyle->getName() . ';' . "\r\n";
  494.         $html .= '        font-size: '             . $pStyle->getSize() . 'pt;' . "\r\n";
  495.     
  496.         // Return
  497.         return $html;
  498.     }
  499.     
  500.     /**
  501.      * Create CSS style (PHPExcel_Style_Borders)
  502.      * 
  503.      * @param    PHPExcel_Style_Borders         $pStyle            PHPExcel_Style_Borders
  504.      * @return    string 
  505.      */
  506.     private function _createCSSStyleBorders(PHPExcel_Style_Borders $pStyle) {
  507.         // Construct HTML
  508.         $html = '';
  509.             
  510.         // Create CSS    
  511.         $html .= '        border-bottom: '         . $this->_createCSSStyleBorder($pStyle->getBottom()) . ';' . "\r\n";
  512.         $html .= '        border-top: '         . $this->_createCSSStyleBorder($pStyle->getTop()) . ';' . "\r\n";
  513.         $html .= '        border-left: '         . $this->_createCSSStyleBorder($pStyle->getLeft()) . ';' . "\r\n";
  514.         $html .= '        border-right: '         . $this->_createCSSStyleBorder($pStyle->getRight()) . ';' . "\r\n";
  515.  
  516.         // Return
  517.         return $html;
  518.     }
  519.     
  520.     /**
  521.      * Create CSS style (PHPExcel_Style_Border)
  522.      * 
  523.      * @param    PHPExcel_Style_Border        $pStyle            PHPExcel_Style_Border
  524.      * @return    string 
  525.      */
  526.     private function _createCSSStyleBorder(PHPExcel_Style_Border $pStyle) {
  527.         // Construct HTML
  528.         $html = '';
  529.             
  530.         // Create CSS
  531.         $html .= $this->_mapBorderStyle($pStyle->getBorderStyle()) . ' #' . $pStyle->getColor()->getRGB();
  532.         
  533.         // Return
  534.         return $html;
  535.     }
  536.     
  537.     /**
  538.      * Create CSS style (PHPExcel_Style_Fill)
  539.      * 
  540.      * @param    PHPExcel_Style_Fill        $pStyle            PHPExcel_Style_Fill
  541.      * @return    string 
  542.      */
  543.     private function _createCSSStyleFill(PHPExcel_Style_Fill $pStyle) {
  544.         // Construct HTML
  545.         $html = '';
  546.             
  547.         // Create CSS
  548.         $html .= '        background-color: '     . '#' . $pStyle->getStartColor()->getRGB() . ';' . "\r\n";
  549.  
  550.         // Return
  551.         return $html;
  552.     }
  553.     
  554.     /**
  555.      * Generate HTML footer
  556.      */
  557.     public function generateHTMLFooter() {
  558.         // Construct HTML
  559.         $html = '';
  560.         $html .= '  </body>' . "\r\n";
  561.         $html .= '</html>' . "\r\n";            
  562.  
  563.         // Return
  564.         return $html;
  565.     }
  566.  
  567.     /**
  568.      * Generate table header
  569.      * 
  570.      * @param     string    $pIdentifier    Identifier for the table
  571.      * @return    string 
  572.      * @throws    Exception
  573.      */
  574.     private function _generateTableHeader($pIdentifier = '') {
  575.         // Construct HTML
  576.         $html = '';
  577.         $html .= '    <table border="0" cellpadding="0" cellspacing="0" class="sheet' . $pIdentifier . '">' . "\r\n";
  578.  
  579.         // Return
  580.         return $html;
  581.     }
  582.     
  583.     /**
  584.      * Generate table footer
  585.      * 
  586.      * @throws    Exception
  587.      */
  588.     private function _generateTableFooter() {
  589.         // Construct HTML
  590.         $html = '';
  591.         $html .= '    </table>' . "\r\n";        
  592.  
  593.         // Return
  594.         return $html;
  595.     }
  596.     
  597.     /**
  598.      * Generate row
  599.      * 
  600.      * @param    PHPExcel_Worksheet     $pSheet            PHPExcel_Worksheet
  601.      * @param    array                $pValues        Array containing cells in a row
  602.      * @param    int                    $pRow            Row number
  603.      * @return    string 
  604.      * @throws    Exception
  605.      */
  606.     private function _generateRow(PHPExcel_Worksheet $pSheet, $pValues = null, $pRow = 0) {
  607.         if (is_array($pValues)) {    
  608.             // Construct HTML
  609.             $html = '';
  610.         
  611.             // Write row start
  612.             $html .= '        <tr class="row' . $pRow . '">' . "\r\n";
  613.             
  614.             // Write cells
  615.             $colNum = 0;
  616.             foreach ($pValues as $cell) {
  617.                 $cellData = '&nbsp;';
  618.                 $cssClass = 'column' . $colNum;
  619.                 $colSpan = 1;
  620.                 $rowSpan = 1;
  621.                 $writeCell = true;    // Write cell
  622.                 
  623.                 // PHPExcel_Cell
  624.                 if ($cell instanceof PHPExcel_Cell) {
  625.                     // Value
  626.                     if ($cell->getValue() instanceof PHPExcel_RichText) {
  627.                         // Loop trough rich text elements
  628.                         $elements = $cell->getValue()->getRichTextElements();
  629.                         foreach ($elements as $element) {
  630.                             // Rich text start?
  631.                             if ($element instanceof PHPExcel_RichText_Run) {
  632.                                 $cellData .= '<span style="' . 
  633.                                     str_replace("\r\n", '',
  634.                                         $this->_createCSSStyleFont($element->getFont())
  635.                                     ) . '">';
  636.                             }
  637.                             
  638.                             // Decode UTF8 data
  639.                             $cellText = $element->getText();
  640.                             if (PHPExcel_Shared_String::IsUTF8($cellText)) {
  641.                                 $cellData .= utf8_decode($cellText);
  642.                             }
  643.                             
  644.                             if ($element instanceof PHPExcel_RichText_Run) {
  645.                                 $cellData .= '</span>';
  646.                             }
  647.                         }
  648.                     } else {
  649.                         if ($this->_preCalculateFormulas) {
  650.                             $cellData = PHPExcel_Style_NumberFormat::toFormattedString(
  651.                                 $cell->getCalculatedValue(),
  652.                                 $pSheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  653.                             );
  654.                         } else {
  655.                             $cellData = PHPExcel_Style_NumberFormat::ToFormattedString(
  656.                                 $cell->getValue(),
  657.                                 $pSheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  658.                             );
  659.                         }
  660.  
  661.                         // Decode UTF8 data
  662.                         if (PHPExcel_Shared_String::IsUTF8($cellData)) {
  663.                             $cellData = utf8_decode($cellData);
  664.                         }
  665.                     }
  666.                     
  667.                     // Check value
  668.                     if ($cellData == '') {
  669.                         $cellData = '&nbsp;';
  670.                     }
  671.                     
  672.                     // Extend CSS class?
  673.                     if (array_key_exists($cell->getCoordinate(), $pSheet->getStyles())) {
  674.                         $cssClass .= ' style' . $pSheet->getStyle($cell->getCoordinate())->getHashCode();
  675.                     }
  676.                 } else {
  677.                     $cell = new PHPExcel_Cell(
  678.                         PHPExcel_Cell::stringFromColumnIndex($colNum),
  679.                         ($pRow + 1),
  680.                         '',
  681.                         null,
  682.                         null
  683.                     );
  684.                 }
  685.                 
  686.                 // Hyperlink?
  687.                 if ($cell->hasHyperlink() && !$cell->getHyperlink()->isInternal()) {
  688.                     $cellData = '<a href="' . $cell->getHyperlink()->getUrl() . '" title="' . $cell->getHyperlink()->getTooltip() . '">' . $cellData . '</a>';
  689.                 }
  690.                 
  691.                 // Column/rowspan
  692.                 foreach ($pSheet->getMergeCells() as $cells) {
  693.                     if ($cell->isInRange($cells)) {
  694.                         list($first, ) = PHPExcel_Cell::splitRange($cells);
  695.                         
  696.                         if ($first == $cell->getCoordinate()) {
  697.                             list($colSpan, $rowSpan) = PHPExcel_Cell::rangeDimension($cells);
  698.                         } else {
  699.                             $writeCell = false;
  700.                         }
  701.                             
  702.                         break;
  703.                     }
  704.                 }
  705.                 
  706.                 // Write
  707.                 if ($writeCell) {
  708.                     // Column start
  709.                     $html .= '          <td';
  710.                         $html .= ' class="' . $cssClass . '"';
  711.                         if ($colSpan > 1) {
  712.                             $html .= ' colspan="' . $colSpan . '"';
  713.                         }
  714.                         if ($rowSpan > 1) {
  715.                             $html .= ' rowspan="' . $rowSpan . '"';
  716.                         }
  717.                     $html .= '>';
  718.                     
  719.                     // Image?
  720.                     $html .= $this->_writeImageTagInCell($pSheet, $cell->getCoordinate());
  721.  
  722.                     // Cell data
  723.                     $html .= $cellData;
  724.                     
  725.                     // Column end
  726.                     $html .= '</td>' . "\r\n";
  727.                 }
  728.                 
  729.                 // Next column
  730.                 $colNum++;
  731.             }
  732.             
  733.             // Write row end
  734.             $html .= '        </tr>' . "\r\n";
  735.             
  736.             // Return
  737.             return $html;
  738.         } else {
  739.             throw new Exception("Invalid parameters passed.");
  740.         }
  741.     }
  742.     
  743.  
  744.     /**
  745.      * Get Pre-Calculate Formulas
  746.      *
  747.      * @return boolean 
  748.      */
  749.     public function getPreCalculateFormulas() {
  750.         return $this->_preCalculateFormulas;
  751.     }
  752.     
  753.     /**
  754.      * Set Pre-Calculate Formulas
  755.      *
  756.      * @param boolean $pValue    Pre-Calculate Formulas?
  757.      */
  758.     public function setPreCalculateFormulas($pValue = true) {
  759.         $this->_preCalculateFormulas = $pValue;
  760.     }
  761.     
  762.     /**
  763.      * Get images root
  764.      *
  765.      * @return string 
  766.      */
  767.     public function getImagesRoot() {
  768.         return $this->_imagesRoot;
  769.     }
  770.     
  771.     /**
  772.      * Set images root
  773.      *
  774.      * @param string $pValue 
  775.      */
  776.     public function setImagesRoot($pValue = '.') {
  777.         $this->_imagesRoot = $pValue;
  778.     }
  779. }

Documentation generated on Mon, 25 Aug 2008 13:43:00 +0200 by phpDocumentor 1.4.1