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

Source for file ExcelWriterXML_Sheet.php

Documentation is available at ExcelWriterXML_Sheet.php

  1. <?php
  2. /**
  3.  * File contains the class files for ExcelWriterXML_Sheet
  4.  * @package ExcelWriterXML
  5.  */
  6.  
  7. /**
  8.  * Class for generating sheets within the Excel document
  9.  * @link http://msdn.microsoft.com/en-us/library/aa140066(office.10).aspx
  10.  * @author Robert F Greer
  11.  * @version 1.0
  12.  * @package ExcelWriterXML
  13.  * @uses ExcelWriterXML_Style::alignHorizontal()
  14.  * @uses ExcelWriterXML_Style::alignRotate()
  15.  * @uses ExcelWriterXML_Style::alignShrinktofit()
  16.  * @uses ExcelWriterXML_Style::alignVertical()
  17.  * @uses ExcelWriterXML_Style::alignVerticaltext()
  18.  * @uses ExcelWriterXML_Style::alignWraptext()
  19.  * @uses ExcelWriterXML_Style::bgColor()
  20.  * @uses ExcelWriterXML_Style::bgPattern()
  21.  * @uses ExcelWriterXML_Style::bgPatternColor()
  22.  * @uses ExcelWriterXML_Style::border()
  23.  * @uses ExcelWriterXML_Style::checkColor()
  24.  * @uses ExcelWriterXML_Style::fontBold()
  25.  * @uses ExcelWriterXML_Style::fontColor()
  26.  * @uses ExcelWriterXML_Style::fontFamily()
  27.  * @uses ExcelWriterXML_Style::fontItalic()
  28.  * @uses ExcelWriterXML_Style::fontName()
  29.  * @uses ExcelWriterXML_Style::fontOutline()
  30.  * @uses ExcelWriterXML_Style::fontShadow()
  31.  * @uses ExcelWriterXML_Style::fontStrikethrough()
  32.  * @uses ExcelWriterXML_Style::fontUnderline()
  33.  * @uses ExcelWriterXML_Style::getErrors()
  34.  * @uses ExcelWriterXML_Style::getID()
  35.  * @uses ExcelWriterXML_Style::getStyleXML()
  36.  * @uses ExcelWriterXML_Style::name()
  37.  * @uses ExcelWriterXML_Style::numberFormat()
  38.  * @uses ExcelWriterXML_Style::numberFormatDate()
  39.  * @uses ExcelWriterXML_Style::numberFormatDatetime()
  40.  * @uses ExcelWriterXML_Style::numberFormatTime()
  41.  */
  42.     // Private Variables
  43.     private $id;
  44.     private $cells array();
  45.     private $colWidth array();
  46.     private $rowHeight array();
  47.     private $URLs array();
  48.     private $mergeCells array();
  49.     private $comments array();
  50.     public  $formatErrors = array();
  51.     private $displayRightToLeft false;
  52.     /////////////////////
  53.     
  54.     // Public Variables
  55.     /////////////////////
  56.     
  57.     // Constructor
  58.     /**
  59.      * Constructor for a new Sheet
  60.      * @param string $id The name of the sheet to be referenced within the
  61.      *  spreadsheet
  62.      */
  63.     function ExcelWriterXML_Sheet($id){
  64.         $this->id $id;
  65.     }
  66.     
  67.     /**
  68.      * Function to get the named value of the Sheet
  69.      * @return string Name of the Sheet
  70.      */
  71.     function getID(){
  72.         return $this->id;
  73.     }
  74.     
  75.     /**
  76.      * Adds a format error.  When the document is generated if there are any
  77.      * errors they will be listed on a seperate sheet.
  78. @param string $function The name of the function that was called
  79.      * @param string $message Details of the error
  80.      */
  81.     public function addError($function$message){
  82.         $tmp array(
  83.             'sheet'        => $this->id,
  84.             'function'    => $function,
  85.             'message'    => $message,
  86.         );
  87.         $this->formatErrors[$tmp;
  88.     }
  89.     
  90.     /**
  91.      * Returns any errors found in the sheet
  92.      * @return mixed Array of errors if they exist, otherwise false
  93.      */
  94.     public function getErrors(){
  95.         return($this->formatErrors);
  96.     }
  97.  
  98.     /**
  99.      * Converts a MySQL type datetime field to a value that can be used within
  100.      * Excel.
  101.      * If the passed value is not valid then the passed string is sent back.
  102.      * @param string $datetime Value must in in the format "yyyy-mm-dd hh:ii:ss"
  103.      * @return string Value in the Excel format "yyyy-mm-ddThh:ii:ss.000"
  104.      */
  105.     public function convertMysqlDatetime($datetime){
  106.         $datetime trim($datetime);
  107.         $pattern "/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/";
  108.         if (preg_match($pattern$datetime$matches)) {
  109.             $datetime $matches[0];
  110.             list($date,$timeexplode(' ',$datetime);
  111.             return($date.'T'.$time.'.000');
  112.         }
  113.         else{
  114.             return($datetime);
  115.         }
  116.     }
  117.     
  118.     /**
  119.      * Converts a MySQL type date field to a value that can be used within Excel
  120.      * If the passed value is not valid then the passed string is sent back.
  121.      * @param string $datetime Value must in in the format "yyyy-mm-dd hh:ii:ss"
  122.      *  or "yyyy-mm-dd"
  123.      * @return string Value in the Excel format "yyyy-mm-ddT00:00:00.000"
  124.      */
  125.     public function convertMysqlDate($datetime){
  126.         $datetime trim($datetime);
  127.         $pattern1 "/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/";
  128.         $pattern2 "/[0-9]{4}-[0-9]{2}-[0-9]{2}/";
  129.         if (preg_match($pattern1$datetime$matches)) {
  130.             $datetime $matches[0];
  131.             list($date,$timeexplode(' ',$datetime);
  132.             return($date.'T'.$time.'.000');
  133.         }
  134.         else if (preg_match($pattern2$datetime$matches)) {
  135.             $date $matches[0];
  136.             return($date.'T00:00:00.000');
  137.         }
  138.         else{
  139.             return($datetime);
  140.         }
  141.     }
  142.     
  143.     /**
  144.      * Converts a MySQL type time field to a value that can be used within Excel
  145.      * If the passed value is not valid then the passed string is sent back.
  146.      * @param string $datetime Value must in in the format "yyyy-mm-dd hh:ii:ss"
  147.      *  or "hh:ii:ss"
  148.      * @return string Value in the Excel format "1899-12-31Thh:ii:ss.000"
  149.      */
  150.     public function convertMysqlTime($datetime){
  151.         $datetime trim($datetime);
  152.         $pattern1 "/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}/";
  153.         $pattern2 "/[0-9]{2}:[0-9]{2}:[0-9]{2}/";
  154.         if (preg_match($pattern1$datetime$matches)) {
  155.             $datetime $matches[0];
  156.             list($date,$timeexplode(' ',$datetime);
  157.             return($date.'T'.$time.'.000');
  158.         }
  159.         else if (preg_match($pattern2$datetime$matches)) {
  160.             $time $matches[0];
  161.             return('1899-12-31T'.$time.'.000');
  162.         }
  163.         else{
  164.             return($datetime);
  165.         }
  166.     }
  167.     
  168.  
  169.     /**
  170.      * Writes a formula to a cell
  171.      * From MS
  172.      * Specifies the formula stored in this cell. All formulas are persisted in
  173.      * R1C1 notation because they are significantly easier to parse and generate
  174.      * than A1-style formulas. The formula is calculated upon reload unless
  175.      * calculation is set to manual. Recalculation of the formula overrides the
  176.      * value in this cell's Value attribute.
  177.      * @see writeFormula()
  178.      * @param string $dataType Type of data that the formula should generate,
  179.      *  "String" "Number" "DateTime"
  180.      * @param integer $row Row, based upon a "1" based array
  181.      * @param integer $column Column, based upon a "1" based array
  182.      * @param string $data Formula data to be written to a cell
  183.      * @param mixed $style Named style, or style reference to be applied to the
  184.      *  cell
  185.      */
  186.     public function writeFormula($dataType,$row,$column,$data,$style null){
  187.         if ($dataType != 'String'
  188.             && $dataType != 'Number'
  189.             && $dataType != 'DateTime'){
  190.             $this->addError(__FUNCTION__,'('.$row.','.$column.') DataType for formula was not valid "'.$dataType.'"');
  191.             $halign 'Automatic';
  192.         }
  193.  
  194.         $this->writeData('String',$row,$column,'',$style,$data);
  195.     }
  196.  
  197.     /**
  198.      * Writes a string to a cell
  199.      * @see writeData()
  200.      * @param integer $row Row, based upon a "1" based array
  201.      * @param integer $column Column, based upon a "1" based array
  202.      * @param string $data String data to be written to a cell
  203.      * @param mixed $style Named style, or style reference to be applied to the
  204.      *  cell
  205.      */
  206.     public function writeString($row,$column,$data,$style null){
  207.         $this->writeData('String',$row,$column,$data,$style);
  208.     }
  209.     
  210.     /**
  211.      * Writes a number to a cell.
  212.      * If the data is not numeric then the function will write the data as a
  213.      * string.
  214.      * @see writeData()
  215.      * @param integer $row Row, based upon a "1" based array
  216.      * @param integer $column Column, based upon a "1" based array
  217.      * @param mixed $data Number data to be written to a cell
  218.      * @param mixed $style Named style, or style reference to be applied to the
  219.      *  cell
  220.      */
  221.     public function writeNumber($row,$column,$data,$style null){
  222.         if (!is_numeric($data)){
  223.             $this->writeData('String',$row,$column,$data,$style);
  224.             $this->addError(__FUNCTION__,'('.$row.','.$column.') Tried to write non-numeric data to type Number "'.$data.'"');
  225.         }
  226.         else{
  227.             $this->writeData('Number',$row,$column,$data,$style);
  228.         }
  229.     }
  230.     
  231.     /**
  232.      * Writes a Date/Time to a cell.
  233.      * If data is not valid the function will write the passed value as a
  234.      * string.
  235.      * @see writeData()
  236.      * @param integer $row Row, based upon a "1" based array
  237.      * @param integer $column Column, based upon a "1" based array
  238.      * @param string $data Date or Time data to be written to a cell.  This must
  239.      *  be in the format "yyyy-mm-ddThh:ii:ss.000" for Excel to recognize it.
  240.      * @param mixed $style Named style, or style reference to be applied to the
  241.      *  cell
  242.      */
  243.     public function writeDateTime($row,$column,$data,$style null){
  244.         $pattern "/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.000/";
  245.         if (preg_match($pattern$data$matches)) {
  246.             $data $matches[0];
  247.             $this->writeData('DateTime',$row,$column,$data,$style);
  248.         }
  249.         else{
  250.             $this->writeData('String',$row,$column,$data,$style);
  251.             $this->addError(__FUNCTION__,'('.$row.','.$column.') Tried to write invalid datetime data to type DateTime "'.$data.'"');
  252.         }
  253.     }
  254.     private function writeData($type,$row,$column,$data,$style null,$formula null){
  255.         if ($style != null){
  256.             if (gettype($style== 'object'){
  257.                 if (get_class($style== 'ExcelWriterXML_Style'){
  258.                     $styleID $style->getID();
  259.                 }
  260.                 else{
  261.                     $this->addError(__FUNCTION__,'('.$row.','.$column.') StyleID supplied was an object, but not a style object "'.get_class($style).'"');
  262.                     $styleID null;
  263.                 }
  264.             }
  265.             else{
  266.                 $styleID $style;
  267.             }
  268.         }
  269.         else{
  270.             $styleID null;
  271.         }
  272.         
  273.         $cell array(
  274.             'type'        => $type,
  275.             'style'        => $styleID,
  276.             'data'        => $data,
  277.             'formula'    => $formula,
  278.         );
  279.         $this->cells[$row][$column$cell;
  280.     }
  281.     
  282.     /**
  283.      * Displays the sheet in Right to Left format
  284.      */
  285.     public function displayRightToLeft(){
  286.         $this->displayRightToLeft true;
  287.     }
  288.  
  289.     /**
  290.      * Called by the ExcelWriterXML class to get the XML data for this object
  291.      * @return string Contains only the XML data for the sheet
  292.      */
  293.     public function getSheetXML(){
  294.         ksort($this->cells);
  295.         
  296.         $displayRightToLeft ($this->displayRightToLeft'ss:RightToLeft="1"' '';
  297.         
  298.         $xml '<Worksheet ss:Name="'.$this->id.'" '.$displayRightToLeft.'>'."\r";
  299.         $xml .= '    <Table>'."\r";
  300.         foreach($this->colWidth as $colIndex => $colWidth){
  301.               $xml .= '        <Column ss:Index="'.$colIndex.'" ss:AutoFitWidth="0" ss:Width="'.$colWidth.'"/>'."\r";
  302.         }
  303.         foreach($this->cells as $row => $rowData){
  304.             ksort($rowData);
  305.             if (isset($this->rowHeight[$row])){
  306.                 $rowHeight 'ss:AutoFitHeight="0" ss:Height="'.$this->rowHeight[$row].'"';
  307.             }
  308.             else{$rowHeight '';}
  309.             $xml .= '        <Row ss:Index="'.$row.'" '.$rowHeight.' >'."\r";
  310.             foreach($rowData as $column => $cell){
  311.                 if (!empty($cell['formula'])) $formula 'ss:Formula="'.$cell['formula'].'"';
  312.                 else $formula '';
  313.                 if (!empty($cell['style'])) $style 'ss:StyleID="'.$cell['style'].'"';
  314.                 else $style '';
  315.                 if (empty($this->URLs[$row][$column])) $URL '';
  316.                 else $URL 'ss:HRef="'.htmlspecialchars($this->URLs[$row][$column]).'"';
  317.                 if (empty($this->mergeCells[$row][$column])) $mergeCell '';
  318.                 else $mergeCell 'ss:MergeAcross="'.$this->mergeCells[$row][$column]['width'].'" ss:MergeDown="'.$this->mergeCells[$row][$column]['height'].'"';
  319.                 if (empty($this->comments[$row][$column])) $comment '';
  320.                 else{
  321.                     $comment '                    <Comment ss:Author="'.$this->comments[$row][$column]['author'].'">'."\r";
  322.                     $comment .= '                    <ss:Data xmlns="http://www.w3.org/TR/REC-html40">'."\r";
  323.                     $comment .= '                    <B><Font html:Face="Tahoma" x:CharSet="1" html:Size="8" html:Color="#000000">'.htmlspecialchars($this->comments[$row][$column]['author']).':</Font></B>'."\r";
  324.                     $comment .= '                    <Font html:Face="Tahoma" x:CharSet="1" html:Size="8" html:Color="#000000">'.htmlspecialchars($this->comments[$row][$column]['comment']).'</Font>'."\r";
  325.                     $comment .= '                    </ss:Data>'."\r";
  326.                     $comment .= '                    </Comment>'."\r";
  327.                 }
  328.                 $type $cell['type'];
  329.                 $data $cell['data'];
  330.                 $data htmlspecialchars($data);
  331.                 $data str_replace("\r\n",'&#10;',$data);
  332.                 $data str_replace("\n",'&#10;',$data);
  333.                 
  334.                 $xml .= '            <Cell '.$style.' ss:Index="'.$column.'" '.$URL.' '.$mergeCell.' '.$formula.'>'."\r";
  335.                 $xml .= '                <Data ss:Type="'.$type.'">';
  336.                 $xml .= $data;
  337.                 $xml .= '</Data>'."\r";
  338.                 $xml .= $comment;
  339.                 $xml .= '            </Cell>'."\r";
  340.             }
  341.             $xml .= '        </Row>'."\r";
  342.         }
  343.         $xml .= '    </Table>'."\r";
  344.         $xml .= '</Worksheet>'."\r";
  345.         return($xml);
  346.     }
  347.  
  348.     /**
  349.      * Alias for function columnWidth()
  350.      */
  351.     function cellWidth$row$col,$width 48){$this->columnWidth($col,$width);}
  352.  
  353.     /**
  354.      * Sets the width of a cell.
  355.      * Sets  the width of the column that the cell resides in.
  356.      * Cell width of zero effectively hides the column
  357.      * @param integer $col Column, based upon a "1" based array
  358.      * @param mixed $width Width of the cell/column, default is 48
  359.      */
  360.     function columnWidth$col,$width 48){$this->colWidth[$col$width;}
  361.  
  362.     /**
  363.      * Alias for function rowHeight()
  364.      */
  365.     function cellHeight$row$col,$height 12.5){$this->rowHeight($row,$height);}
  366.  
  367.     /**
  368.      * Sets the height of a cell.
  369.      * Sets  the height of the column that the cell resides in.
  370.      * Cell height of zero effectively hides the row
  371.      * @param integer $row Row, based upon a "1" based array
  372.      * @param integer $col Column, based upon a "1" based array
  373.      * @param mixed $height Height of the cell/column, default is 12.5
  374.      */
  375.     function rowHeight$row,$height 12.5){$this->rowHeight[$row$height;}
  376.  
  377.     /**
  378.      * Makes the target cell a link to a URL
  379.      * @param integer $row Row, based upon a "1" based array
  380.      * @param integer $col Column, based upon a "1" based array
  381.      * @param string $URL The URL that the link should point to
  382.      */
  383.     function addURL$row$col,$URL){$this->URLs[$row][$col$URL;}
  384.     
  385.     /**
  386.      * Merges 2 or more cells.
  387.      * The function acts like a bounding box, with the row and column defining
  388.      * the upper left corner, and the width and height extending the box.
  389.      * If width or height are zero (or ommitted) then the function does nothing.
  390.      * @param integer $row Row, based upon a "1" based array
  391.      * @param integer $col Column, based upon a "1" based array
  392.      * @param integer $width Number of cells to the right to merge with
  393.      * @param integer $height Number of cells down to merge with
  394.      */
  395.     function cellMerge($row,$col$width 0$height 0){
  396.         if ($width || $height 0){
  397.             $this->addError(__FUNCTION__,'('.$row.','.$col.') Tried to merge cells with width/height < 0 "(w='.$width.',h='.$height.')"');
  398.             return;
  399.         }
  400.         
  401.         $this->mergeCells[$row][$colarray(
  402.             'width'        => $width,
  403.             'height'    => $height,
  404.         );
  405.         /* I don't think this code is necessary
  406.         if (!isset($cells[$row][$col])){
  407.             $this->writeString($row,$col,'');
  408.         }
  409.         */
  410.     }
  411.     
  412.     /**
  413.      * Adds a comment to a cell
  414.      * @param integer $row Row, based upon a "1" based array
  415.      * @param integer $col Column, based upon a "1" based array
  416.      * @param string $comment The comment to be displayed on the cell
  417.      * @param string $author The comment will show a bold header displaying the
  418.      *  author
  419.      */
  420.     function addComment$row$col,$comment,$author 'SYSTEM'){
  421.         $this->comments[$row][$colarray(
  422.             'comment'    => $comment,
  423.             'author'    => $author,
  424.         );
  425.     }
  426.     
  427.     /**
  428.      * Outputs a MYSQL table or list of tables to an Excel doc
  429.      * @param string $host MySQL host to connect to
  430.      * @param string $username Username to connect with
  431.      * @param string $password Password to connect with
  432.      * @param string $db Database to use
  433.      * @param mixed $table If string, out specific table.  If array, each table will have it's own sheet
  434.      */
  435.     public function mysqlQueryToTable($host,$username,$password,$query){
  436.         EWXcheckDriverDB('mysql');
  437.         if (empty($host))        $this->addError('Database','HOSTNAME is empty');
  438.         if (empty($username))    $this->addError('Database','USERNAME is empty');
  439.         if (count($this->formatErrors0){return false;}
  440.         
  441.         $link mysql_connect($host,$username,$password);
  442.         if (!$link$this->addError('Database','UNABLE to connect to '.$host.'('.mysql_error().')');
  443.         if (count($this->formatErrors0){return false;}
  444.         
  445.         EWXmysqlGenerateByQuery($this,$link,$query);
  446.     }
  447. }
  448. ?>

Documentation generated on Wed, 16 Feb 2011 20:04:36 -0600 by phpDocumentor 1.4.3