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

Source for file CSV.php

Documentation is available at CSV.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.4, 2008-10-27
  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_String */
  39. require_once 'PHPExcel/Shared/String.php';
  40.  
  41.  
  42. /**
  43.  * PHPExcel_Writer_CSV
  44.  *
  45.  * @category   PHPExcel
  46.  * @package    PHPExcel_Writer
  47.  * @copyright  Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  48.  */
  49. class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
  50.     /**
  51.      * PHPExcel object
  52.      *
  53.      * @var PHPExcel 
  54.      */
  55.     private $_phpExcel;
  56.     
  57.     /**
  58.      * Delimiter
  59.      * 
  60.      * @var string 
  61.      */
  62.     private $_delimiter;
  63.     
  64.     /**
  65.      * Enclosure
  66.      * 
  67.      * @var string 
  68.      */
  69.     private $_enclosure;
  70.     
  71.     /**
  72.      * Line ending
  73.      * 
  74.      * @var string 
  75.      */
  76.     private $_lineEnding;
  77.     
  78.     /**
  79.      * Sheet index to write
  80.      * 
  81.      * @var int 
  82.      */
  83.     private $_sheetIndex;
  84.     
  85.     /**
  86.      * Pre-calculate formulas
  87.      *
  88.      * @var boolean 
  89.      */
  90.     private $_preCalculateFormulas = true;
  91.     
  92.     /**
  93.      * Create a new PHPExcel_Writer_CSV
  94.      *
  95.      * @param     PHPExcel    $phpExcel    PHPExcel object
  96.      */
  97.     public function __construct(PHPExcel $phpExcel{
  98.         $this->_phpExcel     = $phpExcel;
  99.         $this->_delimiter     = ',';
  100.         $this->_enclosure     = '"';
  101.         $this->_lineEnding     = PHP_EOL;
  102.         $this->_sheetIndex     = 0;
  103.     }
  104.     
  105.     /**
  106.      * Save PHPExcel to file
  107.      *
  108.      * @param     string         $pFileName 
  109.      * @throws     Exception
  110.      */    
  111.     public function save($pFilename null{
  112.         // Fetch sheet
  113.         $sheet $this->_phpExcel->getSheet($this->_sheetIndex);
  114.         
  115.         // Open file
  116.         $fileHandle fopen($pFilename'w');
  117.         if ($fileHandle === false{
  118.             throw new Exception("Could not open file $pFilename for writing.");
  119.         }
  120.         
  121.         // Convert sheet to array
  122.         $cellsArray $sheet->toArray(''$this->_preCalculateFormulas);
  123.         
  124.         // Write rows to file
  125.         foreach ($cellsArray as $row{
  126.             $this->_writeLine($fileHandle$row);
  127.         }
  128.                 
  129.         // Close file
  130.         fclose($fileHandle);
  131.     }
  132.     
  133.     /**
  134.      * Get delimiter
  135.      * 
  136.      * @return string 
  137.      */
  138.     public function getDelimiter({
  139.         return $this->_delimiter;
  140.     }
  141.     
  142.     /**
  143.      * Set delimiter
  144.      * 
  145.      * @param    string    $pValue        Delimiter, defaults to ,
  146.      */
  147.     public function setDelimiter($pValue ','{
  148.         $this->_delimiter = $pValue;
  149.     }
  150.     
  151.     /**
  152.      * Get enclosure
  153.      * 
  154.      * @return string 
  155.      */
  156.     public function getEnclosure({
  157.         return $this->_enclosure;
  158.     }
  159.     
  160.     /**
  161.      * Set enclosure
  162.      * 
  163.      * @param    string    $pValue        Enclosure, defaults to "
  164.      */
  165.     public function setEnclosure($pValue '"'{
  166.         if ($pValue == ''{
  167.             $pValue null;
  168.         }
  169.         $this->_enclosure = $pValue;
  170.     }
  171.     
  172.     /**
  173.      * Get line ending
  174.      * 
  175.      * @return string 
  176.      */
  177.     public function getLineEnding({
  178.         return $this->_lineEnding;
  179.     }
  180.     
  181.     /**
  182.      * Set line ending
  183.      * 
  184.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  185.      */
  186.     public function setLineEnding($pValue PHP_EOL{
  187.         $this->_lineEnding = $pValue;
  188.     }
  189.     
  190.     /**
  191.      * Get sheet index
  192.      * 
  193.      * @return int 
  194.      */
  195.     public function getSheetIndex({
  196.         return $this->_sheetIndex;
  197.     }
  198.     
  199.     /**
  200.      * Set sheet index
  201.      * 
  202.      * @param    int        $pValue        Sheet index
  203.      */
  204.     public function setSheetIndex($pValue 0{
  205.         $this->_sheetIndex = $pValue;
  206.     }
  207.     
  208.     /**
  209.      * Write line to CSV file
  210.      * 
  211.      * @param    mixed    $pFileHandle    PHP filehandle
  212.      * @param    array    $pValues        Array containing values in a row
  213.      * @throws    Exception
  214.      */
  215.     private function _writeLine($pFileHandle null$pValues null{
  216.         if (!is_null($pFileHandle&& is_array($pValues)) {
  217.             // No leading delimiter
  218.             $writeDelimiter false;
  219.             
  220.             // Build the line
  221.             $line '';
  222.             
  223.             foreach ($pValues as $element{
  224.                 // Decode UTF8 data
  225.                 if (PHPExcel_Shared_String::IsUTF8($element)) {
  226.                     $element utf8_decode($element);
  227.                 }
  228.                 
  229.                 // Escape enclosures
  230.                 $element str_replace($this->_enclosure$this->_enclosure . $this->_enclosure$element);
  231.  
  232.                 // Add delimiter
  233.                 if ($writeDelimiter{
  234.                     $line .= $this->_delimiter;
  235.                 else {
  236.                     $writeDelimiter true;
  237.                 }
  238.                 
  239.                 // Add enclosed string
  240.                 $line .= $this->_enclosure . $element $this->_enclosure;
  241.             }
  242.             
  243.             // Add line ending
  244.             $line .= $this->_lineEnding;
  245.             
  246.             // Write to file
  247.             fwrite($pFileHandle$line);
  248.         else {
  249.             throw new Exception("Invalid parameters passed.");
  250.         }
  251.     }
  252.  
  253.     /**
  254.      * Get Pre-Calculate Formulas
  255.      *
  256.      * @return boolean 
  257.      */
  258.     public function getPreCalculateFormulas({
  259.         return $this->_preCalculateFormulas;
  260.     }
  261.     
  262.     /**
  263.      * Set Pre-Calculate Formulas
  264.      *
  265.      * @param boolean $pValue    Pre-Calculate Formulas?
  266.      */
  267.     public function setPreCalculateFormulas($pValue true{
  268.         $this->_preCalculateFormulas = $pValue;
  269.     }
  270. }

Documentation generated on Mon, 27 Oct 2008 08:37:56 +0100 by phpDocumentor 1.4.1