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.7, 2009-04-22
  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.     protected $_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.      * Use inline CSS?
  86.      *
  87.      * @var boolean 
  88.      */
  89.     private $_useInlineCss = false;
  90.     
  91.     /**
  92.      * Array of CSS styles
  93.      *
  94.      * @var array 
  95.      */
  96.     private $_cssStyles = null;
  97.  
  98.     /**
  99.      * Array of column widths in points
  100.      *
  101.      * @var array 
  102.      */
  103.     private $_columnWidths = null;
  104.  
  105.     /**
  106.      * Create a new PHPExcel_Writer_HTML
  107.      *
  108.      * @param     PHPExcel    $phpExcel    PHPExcel object
  109.      */
  110.     public function __construct(PHPExcel $phpExcel{
  111.         $this->_phpExcel = $phpExcel;
  112.         $this->_sheetIndex = 0;
  113.         $this->_imagesRoot = '.';
  114.     }
  115.  
  116.     /**
  117.      * Save PHPExcel to file
  118.      *
  119.      * @param     string         $pFileName 
  120.      * @throws     Exception
  121.      */
  122.     public function save($pFilename null{
  123.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  124.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  125.  
  126.         // Build CSS
  127.         $this->buildCSS(!$this->_useInlineCss);
  128.         
  129.         // Open file
  130.         $fileHandle fopen($pFilename'w');
  131.         if ($fileHandle === false{
  132.             throw new Exception("Could not open file $pFilename for writing.");
  133.         }
  134.  
  135.         // Write headers
  136.         fwrite($fileHandle$this->generateHTMLHeader(!$this->_useInlineCss));
  137.  
  138.         // Write data
  139.         fwrite($fileHandle$this->generateSheetData());
  140.  
  141.         // Write footer
  142.         fwrite($fileHandle$this->generateHTMLFooter());
  143.  
  144.         // Close file
  145.         fclose($fileHandle);
  146.  
  147.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  148.     }
  149.  
  150.     /**
  151.      * Map VAlign
  152.      */
  153.     private function _mapVAlign($vAlign{
  154.         switch ($vAlign{
  155.             case PHPExcel_Style_Alignment::VERTICAL_BOTTOMreturn 'bottom';
  156.             case PHPExcel_Style_Alignment::VERTICAL_TOPreturn 'top';
  157.             case PHPExcel_Style_Alignment::VERTICAL_CENTER:
  158.             case PHPExcel_Style_Alignment::VERTICAL_JUSTIFYreturn 'middle';
  159.             defaultreturn ' baseline';
  160.         }
  161.     }
  162.  
  163.     /**
  164.      * Map HAlign
  165.      *
  166.      * @return string|false
  167.      */
  168.     private function _mapHAlign($hAlign{
  169.         switch ($hAlign{
  170.             case PHPExcel_Style_Alignment::HORIZONTAL_GENERALreturn false;
  171.             case PHPExcel_Style_Alignment::HORIZONTAL_LEFTreturn 'left';
  172.             case PHPExcel_Style_Alignment::HORIZONTAL_RIGHTreturn 'right';
  173.             case PHPExcel_Style_Alignment::HORIZONTAL_CENTERreturn 'center';
  174.             case PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFYreturn 'justify';
  175.             defaultreturn false;
  176.         }
  177.     }
  178.  
  179.     /**
  180.      * Map border style
  181.      */
  182.     private function _mapBorderStyle($borderStyle{
  183.         switch ($borderStyle{
  184.             case PHPExcel_Style_Border::BORDER_NONEreturn '0px';
  185.             case PHPExcel_Style_Border::BORDER_DASHEDreturn '1px dashed';
  186.             case PHPExcel_Style_Border::BORDER_DOTTEDreturn '1px dotted';
  187.             case PHPExcel_Style_Border::BORDER_THICKreturn '2px solid';
  188.             defaultreturn '1px solid'// map others to thin
  189.         }
  190.     }
  191.  
  192.     /**
  193.      * Get sheet index
  194.      *
  195.      * @return int 
  196.      */
  197.     public function getSheetIndex({
  198.         return $this->_sheetIndex;
  199.     }
  200.  
  201.     /**
  202.      * Set sheet index
  203.      *
  204.      * @param    int        $pValue        Sheet index
  205.      */
  206.     public function setSheetIndex($pValue 0{
  207.         $this->_sheetIndex = $pValue;
  208.     }
  209.  
  210.     /**
  211.      * Write all sheets (resets sheetIndex to NULL)
  212.      */
  213.     public function writeAllSheets({
  214.         $this->_sheetIndex = null;
  215.     }
  216.  
  217.     /**
  218.      * Generate HTML header
  219.      *
  220.      * @param    boolean        $pIncludeStyles        Include styles?
  221.      * @return    string 
  222.      * @throws Exception
  223.      */
  224.     public function generateHTMLHeader($pIncludeStyles false{
  225.         // PHPExcel object known?
  226.         if (is_null($this->_phpExcel)) {
  227.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  228.         }
  229.  
  230.         // Construct HTML
  231.         $html '';
  232.         $html .= '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' "\r\n";
  233.         $html .= '<!-- Generated by PHPExcel - http://www.phpexcel.net -->' "\r\n";
  234.         $html .= '<html>' "\r\n";
  235.         $html .= '  <head>' "\r\n";
  236.         $html .= '    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">' "\r\n";
  237.         $html .= '    <title>' htmlspecialchars($this->_phpExcel->getProperties()->getTitle()) '</title>' "\r\n";
  238.         if ($pIncludeStyles{
  239.             $html .= $this->generateStyles(true);
  240.         }
  241.         $html .= '  </head>' "\r\n";
  242.         $html .= '' "\r\n";
  243.         $html .= '  <body>' "\r\n";
  244.  
  245.         // Return
  246.         return $html;
  247.     }
  248.  
  249.     /**
  250.      * Generate sheet data
  251.      *
  252.      * @return    string 
  253.      * @throws Exception
  254.      */
  255.     public function generateSheetData({
  256.         // PHPExcel object known?
  257.         if (is_null($this->_phpExcel)) {
  258.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  259.         }
  260.  
  261.         // Fetch sheets
  262.         $sheets array();
  263.         if (is_null($this->_sheetIndex)) {
  264.             $sheets $this->_phpExcel->getAllSheets();
  265.         else {
  266.             $sheets[$this->_phpExcel->getSheet($this->_sheetIndex);
  267.         }
  268.  
  269.         // Construct HTML
  270.         $html '';
  271.  
  272.         // Loop all sheets
  273.         foreach ($sheets as $sheet{
  274.             // Get cell collection
  275.             $cellCollection $sheet->getCellCollection();
  276.  
  277.             // Write table header
  278.             $html .= $this->_generateTableHeader($sheet);
  279.  
  280.             // Get worksheet dimension
  281.             $dimension explode(':'$sheet->calculateWorksheetDimension());
  282.             $dimension[0PHPExcel_Cell::coordinateFromString($dimension[0]);
  283.             $dimension[0][0PHPExcel_Cell::columnIndexFromString($dimension[0][0]1;
  284.             $dimension[1PHPExcel_Cell::coordinateFromString($dimension[1]);
  285.             $dimension[1][0PHPExcel_Cell::columnIndexFromString($dimension[1][0]1;
  286.  
  287.             // Loop trough cells
  288.             $rowData null;
  289.             for ($row $dimension[0][1]$row <= $dimension[1][1]++$row{
  290.                 // Start a new row
  291.                 $rowData array();
  292.  
  293.                 // Loop trough columns
  294.                 for ($column $dimension[0][0]$column <= $dimension[1][0]++$column{
  295.                     // Cell exists?
  296.                     if ($sheet->cellExistsByColumnAndRow($column$row)) {
  297.                         $rowData[$column$sheet->getCellByColumnAndRow($column$row);
  298.                     else {
  299.                         $rowData[$column'';
  300.                     }
  301.                 }
  302.  
  303.                 // Write row
  304.                 $html .= $this->_generateRow($sheet$rowData$row 1);
  305.             }
  306.  
  307.             // Write table footer
  308.             $html .= $this->_generateTableFooter();
  309.         }
  310.  
  311.         // Return
  312.         return $html;
  313.     }
  314.  
  315.     /**
  316.      * Generate image tag in cell
  317.      *
  318.      * @param    PHPExcel_Worksheet     $pSheet            PHPExcel_Worksheet
  319.      * @param    string                $coordinates    Cell coordinates
  320.      * @return    string 
  321.      * @throws    Exception
  322.      */
  323.     private function _writeImageTagInCell(PHPExcel_Worksheet $pSheet$coordinates{
  324.         // Construct HTML
  325.         $html '';
  326.  
  327.         // Write images
  328.         foreach ($pSheet->getDrawingCollection(as $drawing{
  329.             if ($drawing instanceof PHPExcel_Worksheet_Drawing{
  330.                 if ($drawing->getCoordinates(== $coordinates{
  331.                     $filename $drawing->getPath();
  332.  
  333.                     // Strip off eventual '.'
  334.                     if (substr($filename01== '.'{
  335.                         $filename substr($filename1);
  336.                     }
  337.  
  338.                     // Prepend images root
  339.                     $filename $this->getImagesRoot($filename;
  340.  
  341.                     // Strip off eventual '.'
  342.                     if (substr($filename01== '.' && substr($filename02!= './'{
  343.                         $filename substr($filename1);
  344.                     }
  345.  
  346.                     // Convert UTF8 data to PCDATA
  347.                     $filename htmlspecialchars($filename);
  348.  
  349.                     $html .= "\r\n";
  350.                     $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" width="' $drawing->getWidth('" height="' $drawing->getHeight('" />' "\r\n";
  351.                 }
  352.             }
  353.         }
  354.  
  355.         // Return
  356.         return $html;
  357.     }
  358.  
  359.     /**
  360.      * Generate CSS styles
  361.      *
  362.      * @param    boolean    $generateSurroundingHTML    Generate surrounding HTML tags? (<style> and </style>)
  363.      * @return    string 
  364.      * @throws    Exception
  365.      */
  366.     public function generateStyles($generateSurroundingHTML true{
  367.         // PHPExcel object known?
  368.         if (is_null($this->_phpExcel)) {
  369.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  370.         }
  371.         
  372.         // Build CSS
  373.         $css $this->buildCSS($generateSurroundingHTML);
  374.  
  375.         // Construct HTML
  376.         $html '';
  377.  
  378.         // Start styles
  379.         if ($generateSurroundingHTML{
  380.             $html .= '    <style type="text/css">' "\r\n";
  381.             $html .= '      html { ' $css['html'' }' "\r\n";
  382.         }
  383.         
  384.         // Write all other styles
  385.         foreach ($css as $styleName => $styleDefinition{
  386.             if ($styleName != 'html'{
  387.                 if (substr($styleName05== 'style'{
  388.                     $styleName '.' $styleName;
  389.                 }
  390.                 $html .= '      ' $styleName ' { ' $styleDefinition ' }' "\r\n";
  391.             }
  392.         }
  393.  
  394.         // End styles
  395.         if ($generateSurroundingHTML{
  396.             $html .= '    </style>' "\r\n";
  397.         }
  398.  
  399.         // Return
  400.         return $html;
  401.     }
  402.     
  403.     /**
  404.      * Build CSS styles
  405.      *
  406.      * @param    boolean    $generateSurroundingHTML    Generate surrounding HTML style? (html { })
  407.      * @return    array 
  408.      * @throws    Exception
  409.      */
  410.     public function buildCSS($generateSurroundingHTML true{
  411.         // PHPExcel object known?
  412.         if (is_null($this->_phpExcel)) {
  413.             throw new Exception('Internal PHPExcel object not set to an instance of an object.');
  414.         }
  415.         
  416.         // Cached?
  417.         if (!is_null($this->_cssStyles)) {
  418.             return $this->_cssStyles;
  419.         }
  420.  
  421.         // Construct CSS
  422.         $css array();
  423.  
  424.         // Start styles
  425.         if ($generateSurroundingHTML{
  426.             // html { }
  427.             $css['html']  'font-family: Calibri, Arial, Helvetica, sans-serif; ';
  428.             $css['html'.= 'font-size: 10pt; ';
  429.             $css['html'.= 'background-color: white; ';
  430.         }
  431.  
  432.         // Fetch sheets
  433.         $sheets array();
  434.         if (is_null($this->_sheetIndex)) {
  435.             $sheets $this->_phpExcel->getAllSheets();
  436.         else {
  437.             $sheets[$this->_phpExcel->getSheet($this->_sheetIndex);
  438.         }
  439.  
  440.         // Build styles per sheet
  441.         foreach ($sheets as $sheet{
  442.             // Calculate hash code
  443.             $hashCode $sheet->getHashCode();
  444.  
  445.             // Build styles
  446.             // table.sheetXXXXXX { }
  447.             $css['table.sheet' $hashCode]  '';
  448.             if ($sheet->getShowGridlines()) {
  449.                 $css['table.sheet' $hashCode.= 'border: 1px dotted black; ';
  450.             }
  451.             $css['table.sheet' $hashCode.= 'page-break-after: always; ';
  452.             
  453.             // table.sheetXXXXXX td { }
  454.             $css['table.sheet' $hashCode ' td'$css['table.sheet' $hashCode];
  455.  
  456.             // Calculate column widths
  457.             $sheet->calculateColumnWidths();
  458.  
  459.             // col elements, initialize
  460.             $highestColumnIndex PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()) 1;
  461.             for ($column 0$column <= $highestColumnIndex++$column{
  462.                 $this->_columnWidths[$hashCode][$column42// approximation
  463.                 $css['table.sheet' $hashCode ' col.col' $column'width: 42pt';
  464.             }
  465.  
  466.             // col elements, loop through columnDimensions and set width
  467.             foreach ($sheet->getColumnDimensions(as $columnDimension{
  468.                 if (($width PHPExcel_Shared_Drawing::cellDimensionToPixels($columnDimension->getWidth())) >= 0{
  469.                     $width PHPExcel_Shared_Drawing::pixelsToPoints($width);
  470.                     $column PHPExcel_Cell::columnIndexFromString($columnDimension->getColumnIndex()) 1;
  471.                     $this->_columnWidths[$hashCode][$column$width;
  472.                     $css['table.sheet' $hashCode ' col.col' $column'width: ' $width 'pt; ';
  473.                     
  474.                     if ($columnDimension->getVisible(=== false{
  475.                         $css['table.sheet' $hashCode ' col.col' $column.= 'visibility: collapse; ';
  476.                         $css['table.sheet' $hashCode ' col.col' $column.= '*display: none; '// target IE6+7
  477.                     }
  478.                 }
  479.             }
  480.  
  481.             // Default row height
  482.             $rowDimension $sheet->getDefaultRowDimension();
  483.  
  484.             // table.sheetXXXXXX tr { }
  485.             $css['table.sheet' $hashCode ' tr''';
  486.             // height is disproportionately large
  487.             $px_height roundPHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) 12 );
  488.             $css['table.sheet' $hashCode ' tr'.= 'height: ' $px_height 'px; ';
  489.             if ($rowDimension->getVisible(=== false{
  490.                 $css['table.sheet' $hashCode ' tr'.= 'display: none; ';
  491.                 $css['table.sheet' $hashCode ' tr'.= 'visibility: hidden; ';
  492.             }
  493.  
  494.             // Calculate row heights
  495.             foreach ($sheet->getRowDimensions(as $rowDimension{
  496.                 // table.sheetXXXXXX tr.rowYYYYYY { }
  497.                 $css['table.sheet' $hashCode ' tr.row' ($rowDimension->getRowIndex(1)'';
  498.                 // height is disproportionately large
  499.                 $px_height roundPHPExcel_Shared_Drawing::cellDimensionToPixels($rowDimension->getRowHeight()) 12 );
  500.                 $css['table.sheet' $hashCode ' tr.row' ($rowDimension->getRowIndex(1).= 'height: ' $px_height 'px; ';
  501.                 if ($rowDimension->getVisible(=== false{
  502.                     $css['table.sheet' $hashCode ' tr.row' ($rowDimension->getRowIndex(1).= 'display: none; ';
  503.                     $css['table.sheet' $hashCode ' tr.row' ($rowDimension->getRowIndex(1).= 'visibility: hidden; ';
  504.                 }
  505.             }
  506.  
  507.             // .b {}
  508.             $css['.b''text-align: center; '// BOOL
  509.  
  510.             // .e {}
  511.             $css['.e''text-align: center; '// ERROR
  512.  
  513.             // .f {}
  514.             $css['.f''text-align: right; '// FORMULA
  515.  
  516.             // .inlineStr {}
  517.             $css['.inlineStr''text-align: left; '// INLINE
  518.  
  519.             // .n {}
  520.             $css['.n''text-align: right; '// NUMERIC
  521.  
  522.             // .s {}
  523.             $css['.s''text-align: left; '// STRING
  524.  
  525.             // Calculate cell style hashes
  526.             $cellStyleHashes new PHPExcel_HashTable();
  527.             $aStyles $sheet->getStyles();
  528.             $cellStyleHashes->addFromSource$aStyles );
  529.             $addedStyles array();
  530.             foreach ($aStyles as $style{
  531.                 if(isset($addedStyles[$style->getHashIndex()])) continue;
  532.                 $css['style' $style->getHashIndex()$this->_createCSSStyle$style );
  533.                 $addedStyles[$style->getHashIndex()true;
  534.             }
  535.         }
  536.  
  537.         // Cache
  538.         if (is_null($this->_cssStyles)) {
  539.             $this->_cssStyles = $css;
  540.         }
  541.         
  542.         // Return
  543.         return $css;
  544.     }
  545.  
  546.     /**
  547.      * Create CSS style
  548.      *
  549.      * @param    PHPExcel_Style         $pStyle            PHPExcel_Style
  550.      * @return    string 
  551.      */
  552.     private function _createCSSStyle(PHPExcel_Style $pStyle{
  553.         // Construct CSS
  554.         $css '';
  555.  
  556.         // Create CSS
  557.         $css .= $this->_createCSSStyleAlignment($pStyle->getAlignment());
  558.         $css .= $this->_createCSSStyleFont($pStyle->getFont());
  559.         $css .= $this->_createCSSStyleBorders($pStyle->getBorders());
  560.         $css .= $this->_createCSSStyleFill($pStyle->getFill());
  561.  
  562.         // Return
  563.         return $css;
  564.     }
  565.  
  566.     /**
  567.      * Create CSS style (PHPExcel_Style_Alignment)
  568.      *
  569.      * @param    PHPExcel_Style_Alignment         $pStyle            PHPExcel_Style_Alignment
  570.      * @return    string 
  571.      */
  572.     private function _createCSSStyleAlignment(PHPExcel_Style_Alignment $pStyle{
  573.         // Construct CSS
  574.         $css '';
  575.  
  576.         // Create CSS
  577.         $css .= 'vertical-align: '     $this->_mapVAlign($pStyle->getVertical()) '; ';
  578.         if ($textAlign $this->_mapHAlign($pStyle->getHorizontal())) {
  579.             $css .= 'text-align: '         $textAlign '; ';
  580.         }
  581.  
  582.         // Return
  583.         return $css;
  584.     }
  585.  
  586.     /**
  587.      * Create CSS style (PHPExcel_Style_Font)
  588.      *
  589.      * @param    PHPExcel_Style_Font         $pStyle            PHPExcel_Style_Font
  590.      * @return    string 
  591.      */
  592.     private function _createCSSStyleFont(PHPExcel_Style_Font $pStyle{
  593.         // Construct CSS
  594.         $css '';
  595.  
  596.         // Create CSS
  597.         if ($pStyle->getBold()) {
  598.             $css .= 'font-weight: bold; ';
  599.         }
  600.         if ($pStyle->getUnderline(!= PHPExcel_Style_Font::UNDERLINE_NONE && $pStyle->getStrikethrough()) {
  601.             $css .= 'text-decoration: underline line-through; ';
  602.         else if ($pStyle->getUnderline(!= PHPExcel_Style_Font::UNDERLINE_NONE{
  603.             $css .= 'text-decoration: underline; ';
  604.         else if ($pStyle->getStrikethrough()) {
  605.             $css .= 'text-decoration: line-through; ';
  606.         }
  607.         if ($pStyle->getItalic()) {
  608.             $css .= 'font-style: italic; ';
  609.         }
  610.  
  611.         $css .= 'color: '                 '#' $pStyle->getColor()->getRGB('; ';
  612.         $css .= 'font-family: '         $pStyle->getName('; ';
  613.         $css .= 'font-size: '             $pStyle->getSize('pt; ';
  614.  
  615.         // Return
  616.         return $css;
  617.     }
  618.  
  619.     /**
  620.      * Create CSS style (PHPExcel_Style_Borders)
  621.      *
  622.      * @param    PHPExcel_Style_Borders         $pStyle            PHPExcel_Style_Borders
  623.      * @return    string 
  624.      */
  625.     private function _createCSSStyleBorders(PHPExcel_Style_Borders $pStyle{
  626.         // Construct CSS
  627.         $css '';
  628.  
  629.         // Create CSS
  630.         $css .= 'border-bottom: '         $this->_createCSSStyleBorder($pStyle->getBottom()) '; ';
  631.         $css .= 'border-top: '             $this->_createCSSStyleBorder($pStyle->getTop()) '; ';
  632.         $css .= 'border-left: '         $this->_createCSSStyleBorder($pStyle->getLeft()) '; ';
  633.         $css .= 'border-right: '         $this->_createCSSStyleBorder($pStyle->getRight()) '; ';
  634.  
  635.         // Return
  636.         return $css;
  637.     }
  638.  
  639.     /**
  640.      * Create CSS style (PHPExcel_Style_Border)
  641.      *
  642.      * @param    PHPExcel_Style_Border        $pStyle            PHPExcel_Style_Border
  643.      * @return    string 
  644.      */
  645.     private function _createCSSStyleBorder(PHPExcel_Style_Border $pStyle{
  646.         // Construct HTML
  647.         $css '';
  648.  
  649.         // Create CSS
  650.         $css .= $this->_mapBorderStyle($pStyle->getBorderStyle()) ' #' $pStyle->getColor()->getRGB();
  651.  
  652.         // Return
  653.         return $css;
  654.     }
  655.  
  656.     /**
  657.      * Create CSS style (PHPExcel_Style_Fill)
  658.      *
  659.      * @param    PHPExcel_Style_Fill        $pStyle            PHPExcel_Style_Fill
  660.      * @return    string 
  661.      */
  662.     private function _createCSSStyleFill(PHPExcel_Style_Fill $pStyle{
  663.         // Construct HTML
  664.         $css '';
  665.  
  666.         // Create CSS
  667.         $value $pStyle->getFillType(== PHPExcel_Style_Fill::FILL_NONE ?
  668.             'white' '#' $pStyle->getStartColor()->getRGB();
  669.         $css .= 'background-color: ' $value '; ';
  670.  
  671.         // Return
  672.         return $css;
  673.     }
  674.  
  675.     /**
  676.      * Generate HTML footer
  677.      */
  678.     public function generateHTMLFooter({
  679.         // Construct HTML
  680.         $html '';
  681.         $html .= '  </body>' "\r\n";
  682.         $html .= '</html>' "\r\n";
  683.  
  684.         // Return
  685.         return $html;
  686.     }
  687.  
  688.     /**
  689.      * Generate table header
  690.      *
  691.      * @param     PHPExcel_Worksheet    $pSheet        The worksheet for the table we are writing
  692.      * @return    string 
  693.      * @throws    Exception
  694.      */
  695.     private function _generateTableHeader($pSheet{
  696.         $identifier $pSheet->getHashCode();
  697.  
  698.         // Construct HTML
  699.         $html '';
  700.         
  701.         if (!$this->_useInlineCss{
  702.             $html .= '    <table border="0" cellpadding="0" cellspacing="0" class="sheet' $identifier '">' "\r\n";
  703.         else {
  704.             $style = isset($this->_cssStyles['table.sheet' $identifier]$this->_cssStyles['table.sheet' $identifier'';
  705.             
  706.             $html .= '    <table border="0" cellpadding="0" cellspacing="0" style="' $style '">' "\r\n";
  707.         }
  708.  
  709.         // Write <col> elements
  710.         $highestColumnIndex PHPExcel_Cell::columnIndexFromString($pSheet->getHighestColumn()) 1;
  711.         for ($i 0$i <= $highestColumnIndex++$i{
  712.             if (!$this->_useInlineCss{
  713.                 $html .= '        <col class="col' $i '">' "\r\n";
  714.             else {
  715.                 $style = isset($this->_cssStyles['table.sheet' $identifier]?
  716.                     $this->_cssStyles['table.sheet' $identifier ' col.col' $i'';
  717.                 $html .= '        <col style="' $style '">' "\r\n";
  718.             }
  719.         }
  720.  
  721.         // Return
  722.         return $html;
  723.     }
  724.  
  725.     /**
  726.      * Generate table footer
  727.      *
  728.      * @throws    Exception
  729.      */
  730.     private function _generateTableFooter({
  731.         // Construct HTML
  732.         $html '';
  733.         $html .= '    </table>' "\r\n";
  734.  
  735.         // Return
  736.         return $html;
  737.     }
  738.  
  739.     /**
  740.      * Generate row
  741.      *
  742.      * @param    PHPExcel_Worksheet     $pSheet            PHPExcel_Worksheet
  743.      * @param    array                $pValues        Array containing cells in a row
  744.      * @param    int                    $pRow            Row number
  745.      * @return    string 
  746.      * @throws    Exception
  747.      */
  748.     private function _generateRow(PHPExcel_Worksheet $pSheet$pValues null$pRow 0{
  749.         if (is_array($pValues)) {
  750.             // Construct HTML
  751.             $html '';
  752.             
  753.             // Sheet hashcode
  754.             $sheetHash $pSheet->getHashCode();
  755.  
  756.             // Write row start
  757.             if (!$this->_useInlineCss{
  758.                 $html .= '        <tr class="row' $pRow '">' "\r\n";
  759.             else {
  760.                 $style = isset($this->_cssStyles['table.sheet' $sheetHash ' tr.row' $pRow]$this->_cssStyles['table.sheet' $sheetHash ' tr.row' $pRow'';
  761.                     
  762.                 $html .= '        <tr style="' $style '">' "\r\n";
  763.             }
  764.  
  765.             // Write cells
  766.             $colNum 0;
  767.             foreach ($pValues as $cell{
  768.                 $cellData '&nbsp;';
  769.                 $cssClass '';
  770.                 if (!$this->_useInlineCss{
  771.                     $cssClass 'column' $colNum;
  772.                 else {
  773.                     $cssClass = isset($this->_cssStyles['table.sheet' $sheetHash ' td.column' $colNum]$this->_cssStyles['table.sheet' $sheetHash ' td.column' $colNum'';
  774.                 }
  775.                 $colSpan 1;
  776.                 $rowSpan 1;
  777.                 $writeCell true;    // Write cell
  778.  
  779.                 // PHPExcel_Cell
  780.                 if ($cell instanceof PHPExcel_Cell{
  781.                     // Value
  782.                     if ($cell->getValue(instanceof PHPExcel_RichText{
  783.                         // Loop trough rich text elements
  784.                         $elements $cell->getValue()->getRichTextElements();
  785.                         foreach ($elements as $element{
  786.                             // Rich text start?
  787.                             if ($element instanceof PHPExcel_RichText_Run{
  788.                                 $cellData .= '<span style="' .
  789.                                     str_replace("\r\n"'',
  790.                                         $this->_createCSSStyleFont($element->getFont())
  791.                                     '">';
  792.  
  793.                                 if ($element->getFont()->getSuperScript()) {
  794.                                     $cellData .= '<sup>';
  795.                                 else if ($element->getFont()->getSubScript()) {
  796.                                     $cellData .= '<sub>';
  797.                                 }
  798.                             }
  799.  
  800.                             // Convert UTF8 data to PCDATA
  801.                             $cellText $element->getText();
  802.                             $cellData .= htmlspecialchars($cellText);
  803.  
  804.                             if ($element instanceof PHPExcel_RichText_Run{
  805.                                 if ($element->getFont()->getSuperScript()) {
  806.                                     $cellData .= '</sup>';
  807.                                 else if ($element->getFont()->getSubScript()) {
  808.                                     $cellData .= '</sub>';
  809.                                 }
  810.  
  811.                                 $cellData .= '</span>';
  812.                             }
  813.                         }
  814.                     else {
  815.                         if ($this->_preCalculateFormulas{
  816.                             $cellData PHPExcel_Style_NumberFormat::toFormattedString(
  817.                                 $cell->getCalculatedValue(),
  818.                                 $pSheet->getstyle$cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  819.                             );
  820.                         else {
  821.                             $cellData PHPExcel_Style_NumberFormat::ToFormattedString(
  822.                                 $cell->getValue(),
  823.                                 $pSheet->getstyle$cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  824.                             );
  825.                         }
  826.  
  827.                         // Convert UTF8 data to PCDATA
  828.                         $cellData htmlspecialchars($cellData);
  829.                     }
  830.  
  831.                     // Check value
  832.                     if ($cellData == ''{
  833.                         $cellData '&nbsp;';
  834.                     }
  835.  
  836.                     // Extend CSS class?
  837.                     if (array_key_exists($cell->getCoordinate()$pSheet->getStyles())) {
  838.                         if (!$this->_useInlineCss{
  839.                             $cssClass .= ' style' $pSheet->getStyle($cell->getCoordinate())->getHashIndex();
  840.                             $cssClass .= ' ' $cell->getDataType();
  841.                         else {
  842.                             $cssClass .= isset($this->_cssStyles['style' $pSheet->getStyle($cell->getCoordinate())->getHashIndex()]?
  843.                                 $this->_cssStyles['style' $pSheet->getStyle($cell->getCoordinate())->getHashIndex()'';
  844.                             
  845.                             // General horizontal alignment: Actual horizontal alignment depends on dataType
  846.                             if ($pSheet->getStyle($cell->getCoordinate())->getAlignment()->getHorizontal(== PHPExcel_Style_Alignment::HORIZONTAL_GENERAL
  847.                                 && isset($this->_cssStyles['.' $cell->getDataType()]))
  848.                             {
  849.                                 if (preg_match('/text-align: [^;]*;/'$cssClass)) {
  850.                                     $cssClass preg_replace('/text-align: [^;]*;/'$this->_cssStyles['.' $cell->getDataType()]$cssClass);
  851.                                 else {
  852.                                     $cssClass .= $this->_cssStyles['.' $cell->getDataType()];
  853.                                 }
  854.                             }
  855.                         }
  856.                     }
  857.                 else {
  858.                     $cell new PHPExcel_Cell(
  859.                         PHPExcel_Cell::stringFromColumnIndex($colNum),
  860.                         ($pRow 1),
  861.                         '',
  862.                         null,
  863.                         null
  864.                     );
  865.                 }
  866.  
  867.                 // Hyperlink?
  868.                 if ($cell->hasHyperlink(&& !$cell->getHyperlink()->isInternal()) {
  869.                     $cellData '<a href="' htmlspecialchars($cell->getHyperlink()->getUrl()) '" title="' htmlspecialchars($cell->getHyperlink()->getTooltip()) '">' $cellData '</a>';
  870.                 }
  871.  
  872.                 // Column/rowspan
  873.                 foreach ($pSheet->getMergeCells(as $cells{
  874.                     if ($cell->isInRange($cells)) {
  875.                         list($firstPHPExcel_Cell::splitRange($cells);
  876.  
  877.                         if ($first[0== $cell->getCoordinate()) {
  878.                             list($colSpan$rowSpanPHPExcel_Cell::rangeDimension($cells);
  879.                         else {
  880.                             $writeCell false;
  881.                         }
  882.  
  883.                         break;
  884.                     }
  885.                 }
  886.  
  887.                 // Write
  888.                 if ($writeCell{
  889.                     // Column start
  890.                     $html .= '          <td';
  891.                         if (!$this->_useInlineCss{
  892.                             $html .= ' class="' $cssClass '"';
  893.                         else {
  894.                             //** Necessary redundant code for the sake of PHPExcel_Writer_PDF **
  895.                             // We must explicitly write the width of the <td> element because TCPDF
  896.                             // does not recognize e.g. <col style="width:42pt">;
  897.                             $width 0;
  898.                             $columnIndex PHPExcel_Cell::columnIndexFromString($cell->getColumn()) 1;
  899.                             for ($i $columnIndex$i $columnIndex $colSpan++$i{
  900.                                 if (isset($this->_columnWidths[$sheetHash][$i])) {
  901.                                     $width += $this->_columnWidths[$sheetHash][$i];
  902.                                 }
  903.                             }
  904.                             $cssClass .= 'width: ' $width 'pt; ';
  905.                             //** end of redundant code **
  906.                             
  907.                             $html .= ' style="' $cssClass '"';
  908.                         }
  909.                         if ($colSpan 1{
  910.                             $html .= ' colspan="' $colSpan '"';
  911.                         }
  912.                         if ($rowSpan 1{
  913.                             $html .= ' rowspan="' $rowSpan '"';
  914.                         }
  915.                     $html .= '>';
  916.  
  917.                     // Image?
  918.                     $html .= $this->_writeImageTagInCell($pSheet$cell->getCoordinate());
  919.  
  920.                     // Cell data
  921.                     $html .= $cellData;
  922.                     
  923.                     // Column end
  924.                     $html .= '</td>' "\r\n";
  925.                 }
  926.  
  927.                 // Next column
  928.                 ++$colNum;
  929.             }
  930.  
  931.             // Write row end
  932.             $html .= '        </tr>' "\r\n";
  933.  
  934.             // Return
  935.             return $html;
  936.         else {
  937.             throw new Exception("Invalid parameters passed.");
  938.         }
  939.     }
  940.  
  941.  
  942.     /**
  943.      * Get Pre-Calculate Formulas
  944.      *
  945.      * @return boolean 
  946.      */
  947.     public function getPreCalculateFormulas({
  948.         return $this->_preCalculateFormulas;
  949.     }
  950.  
  951.     /**
  952.      * Set Pre-Calculate Formulas
  953.      *
  954.      * @param boolean $pValue    Pre-Calculate Formulas?
  955.      */
  956.     public function setPreCalculateFormulas($pValue true{
  957.         $this->_preCalculateFormulas = $pValue;
  958.     }
  959.  
  960.     /**
  961.      * Get images root
  962.      *
  963.      * @return string 
  964.      */
  965.     public function getImagesRoot({
  966.         return $this->_imagesRoot;
  967.     }
  968.  
  969.     /**
  970.      * Set images root
  971.      *
  972.      * @param string $pValue 
  973.      */
  974.     public function setImagesRoot($pValue '.'{
  975.         $this->_imagesRoot = $pValue;
  976.     }
  977.     
  978.     /**
  979.      * Get use inline CSS?
  980.      *
  981.      * @return boolean 
  982.      */
  983.     public function getUseInlineCss({
  984.         return $this->_useInlineCss;
  985.     }
  986.  
  987.     /**
  988.      * Set use inline CSS?
  989.      *
  990.      * @param boolean $pValue 
  991.      */
  992.     public function setUseInlineCss($pValue false{
  993.         $this->_useInlineCss = $pValue;
  994.     }
  995. }

Documentation generated on Wed, 22 Apr 2009 09:00:03 +0200 by phpDocumentor 1.4.1