Location: PHPKode > scripts > Zend Framework Data Grid > zend-framework-data-grid/Bvb/Grid/Deploy/Excel.php
<?php



/**
 * Mascker
 *
 * LICENSE
 *
 * This source file is subject to the GNU General Public License 2.0
 * It is  available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/gpl-2.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to hide@address.com so we can send you a copy immediately.
 *
 * @package    Mascker_Grid
 * @copyright  Copyright (c) Mascker (http://www.petala-azul.com)
 * @license    http://www.opensource.org/licenses/gpl-2.0.php   GNU General Public License 2.0
 * @version    0.1  mascker $
 * @author     Mascker (Bento Vilas Boas) <hide@address.com > 
 */

class Bvb_Grid_Deploy_Excel extends Bvb_Grid_DataGrid
{

    
    protected $output = 'excel';

   protected $dir;

    protected $title;

    protected $options = array ();
    


    function __construct($db, $title, $dir, $options = array('download'))
    {

        
        if (! in_array ( 'excel', $this->export ))
        {
            echo $this->__( "You dont' have permission to export the results to this format" );
            die();
        }
        
         $this->dir = rtrim ( $dir, "/" ) . "/";
        $this->title = $title;
        $this->options = $options;
        
        
        parent::__construct ( $db );
    }


    /**
     * [Para podemros utiliza]
     *
     * @param string $var
     * @param string $value
     */
    
    function __set($var, $value)
    {

        parent::__set ( $var, $value );
    }


    function deploy()
    {

        $this->setPagination(0);
        
        parent::deploy ();
        
        $titles = parent::buildTitles ();
        $wsData = parent::buildGrid ();
        $sql = parent::buildSqlExp ();
        
        /*
        $nome = reset ( $titles );
        
        
        if($nome['field']=='id' || strpos($nome['field'],'_id')  || strpos($nome['field'],'id_')  || strpos($nome['field'],'.id')  )
        {
            @array_shift($titles);
            @array_shift($sql);
            $remove = true;
        }
        
        */
        $xml = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?>
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">';
        

        $xml .= '<Worksheet ss:Name="' . $this->title. '" ss:Description="' . $this->title . '"><ss:Table>';
        
        $xml .= '<ss:Row>';
        foreach ( $titles as $value )
        {
            
            $type = ! is_numeric ( $value ['value'] ) ? 'String' : 'Number';
            
            $xml .= '<ss:Cell><Data ss:Type="' . $type . '">' . $value['value'] . '</Data></ss:Cell>';
        }
        $xml .= '</ss:Row>';
        

        if (is_array ( $wsData ))
        {
            foreach ( $wsData as $row )
            {
                
                $xml .= '<ss:Row>';
                $a = 1;
                foreach ( $row as $value )
                {
                    
                    $value ['value'] = strip_tags ( $value ['value'] );
                    

                    $type = ! is_numeric ( $value ['value'] ) ? 'String' : 'Number';
                    $xml .= '<ss:Cell><Data ss:Type="' . $type . '">' . $value['value'] . '</Data></ss:Cell>';
                    
                    $a ++;
                }
                $xml .= '</ss:Row>';
            }
        
        }
        
        if (is_array ( $sql ))
        {
            $xml .= '<ss:Row>';
            foreach ( $sql as $value )
            {
                
                $type = ! is_numeric ( $value ['value'] ) ? 'String' : 'Number';
                
                $xml .= '<ss:Cell><Data ss:Type="' . $type . '">' . $value['value'] . '</Data></ss:Cell>';
            }
            $xml .= '</ss:Row>';
        }
      

        $xml .= '</ss:Table></Worksheet>';
        

        $xml .= '</Workbook>';
        
  
        file_put_contents ( $this->dir . $this->title . ".xls", $xml );
        
        
        if (in_array ( 'download', $this->options ))
        {
            header ( 'Content-type: application/excel' );
            header ( 'Content-Disposition: attachment; filename="' . $this->title . '.xls"' );
            readfile ( $this->dir . $this->title . '.xls' );
        }
        

        if (! in_array ( 'save', $this->options ))
        {
            unlink ( $this->dir . $this->title . '.xls' );
        }
        
        die ();
        
    }

}




Return current item: Zend Framework Data Grid