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 - 2010 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 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.7.3c, 2010-06-01
  26.  */
  27.  
  28.  
  29. /**
  30.  * PHPExcel_Writer_CSV
  31.  *
  32.  * @category   PHPExcel
  33.  * @package    PHPExcel_Writer
  34.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  35.  */
  36. class PHPExcel_Writer_CSV implements PHPExcel_Writer_IWriter {
  37.     /**
  38.      * PHPExcel object
  39.      *
  40.      * @var PHPExcel 
  41.      */
  42.     private $_phpExcel;
  43.  
  44.     /**
  45.      * Delimiter
  46.      *
  47.      * @var string 
  48.      */
  49.     private $_delimiter;
  50.  
  51.     /**
  52.      * Enclosure
  53.      *
  54.      * @var string 
  55.      */
  56.     private $_enclosure;
  57.  
  58.     /**
  59.      * Line ending
  60.      *
  61.      * @var string 
  62.      */
  63.     private $_lineEnding;
  64.  
  65.     /**
  66.      * Sheet index to write
  67.      *
  68.      * @var int 
  69.      */
  70.     private $_sheetIndex;
  71.  
  72.     /**
  73.      * Pre-calculate formulas
  74.      *
  75.      * @var boolean 
  76.      */
  77.     private $_preCalculateFormulas true;
  78.  
  79.     /**
  80.      * Whether to write a BOM (for UTF8).
  81.      *
  82.      * @var boolean 
  83.      */
  84.     private $_useBOM false;
  85.  
  86.     /**
  87.      * Create a new PHPExcel_Writer_CSV
  88.      *
  89.      * @param     PHPExcel    $phpExcel    PHPExcel object
  90.      */
  91.     public function __construct(PHPExcel $phpExcel{
  92.         $this->_phpExcel     $phpExcel;
  93.         $this->_delimiter     ',';
  94.         $this->_enclosure     '"';
  95.         $this->_lineEnding     PHP_EOL;
  96.         $this->_sheetIndex     0;
  97.     }
  98.  
  99.     /**
  100.      * Save PHPExcel to file
  101.      *
  102.      * @param     string         $pFileName 
  103.      * @throws     Exception
  104.      */
  105.     public function save($pFilename null{
  106.         // Fetch sheet
  107.         $sheet $this->_phpExcel->getSheet($this->_sheetIndex);
  108.  
  109.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  110.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  111.  
  112.         // Open file
  113.         $fileHandle fopen($pFilename'w');
  114.         if ($fileHandle === false{
  115.             throw new Exception("Could not open file $pFilename for writing.");
  116.         }
  117.  
  118.         if ($this->_useBOM{
  119.             // Write the UTF-8 BOM code
  120.             fwrite($fileHandle"\xEF\xBB\xBF");
  121.         }
  122.  
  123.         // Convert sheet to array
  124.         $cellsArray $sheet->toArray(''$this->_preCalculateFormulas);
  125.  
  126.         // Write rows to file
  127.         foreach ($cellsArray as $row{
  128.             $this->_writeLine($fileHandle$row);
  129.         }
  130.  
  131.         // Close file
  132.         fclose($fileHandle);
  133.  
  134.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  135.     }
  136.  
  137.     /**
  138.      * Get delimiter
  139.      *
  140.      * @return string 
  141.      */
  142.     public function getDelimiter({
  143.         return $this->_delimiter;
  144.     }
  145.  
  146.     /**
  147.      * Set delimiter
  148.      *
  149.      * @param    string    $pValue        Delimiter, defaults to ,
  150.      * @return PHPExcel_Writer_CSV 
  151.      */
  152.     public function setDelimiter($pValue ','{
  153.         $this->_delimiter $pValue;
  154.         return $this;
  155.     }
  156.  
  157.     /**
  158.      * Get enclosure
  159.      *
  160.      * @return string 
  161.      */
  162.     public function getEnclosure({
  163.         return $this->_enclosure;
  164.     }
  165.  
  166.     /**
  167.      * Set enclosure
  168.      *
  169.      * @param    string    $pValue        Enclosure, defaults to "
  170.      * @return PHPExcel_Writer_CSV 
  171.      */
  172.     public function setEnclosure($pValue '"'{
  173.         if ($pValue == ''{
  174.             $pValue null;
  175.         }
  176.         $this->_enclosure $pValue;
  177.         return $this;
  178.     }
  179.  
  180.     /**
  181.      * Get line ending
  182.      *
  183.      * @return string 
  184.      */
  185.     public function getLineEnding({
  186.         return $this->_lineEnding;
  187.     }
  188.  
  189.     /**
  190.      * Set line ending
  191.      *
  192.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  193.      * @return PHPExcel_Writer_CSV 
  194.      */
  195.     public function setLineEnding($pValue PHP_EOL{
  196.         $this->_lineEnding $pValue;
  197.         return $this;
  198.     }
  199.  
  200.     /**
  201.      * Get whether BOM should be used
  202.      *
  203.      * @return boolean 
  204.      */
  205.     public function getUseBOM({
  206.         return $this->_useBOM;
  207.     }
  208.  
  209.     /**
  210.      * Set whether BOM should be used
  211.      *
  212.      * @param    boolean    $pValue        Use UTF-8 byte-order mark? Defaults to false
  213.      * @return PHPExcel_Writer_CSV 
  214.      */
  215.     public function setUseBOM($pValue false{
  216.         $this->_useBOM $pValue;
  217.         return $this;
  218.     }
  219.  
  220.     /**
  221.      * Get sheet index
  222.      *
  223.      * @return int 
  224.      */
  225.     public function getSheetIndex({
  226.         return $this->_sheetIndex;
  227.     }
  228.  
  229.     /**
  230.      * Set sheet index
  231.      *
  232.      * @param    int        $pValue        Sheet index
  233.      * @return PHPExcel_Writer_CSV 
  234.      */
  235.     public function setSheetIndex($pValue 0{
  236.         $this->_sheetIndex $pValue;
  237.         return $this;
  238.     }
  239.  
  240.     /**
  241.      * Write line to CSV file
  242.      *
  243.      * @param    mixed    $pFileHandle    PHP filehandle
  244.      * @param    array    $pValues        Array containing values in a row
  245.      * @throws    Exception
  246.      */
  247.     private function _writeLine($pFileHandle null$pValues null{
  248.         if (!is_null($pFileHandle&& is_array($pValues)) {
  249.             // No leading delimiter
  250.             $writeDelimiter false;
  251.  
  252.             // Build the line
  253.             $line '';
  254.  
  255.             foreach ($pValues as $element{
  256.                 // Escape enclosures
  257.                 $element str_replace($this->_enclosure$this->_enclosure $this->_enclosure$element);
  258.  
  259.                 // Add delimiter
  260.                 if ($writeDelimiter{
  261.                     $line .= $this->_delimiter;
  262.                 else {
  263.                     $writeDelimiter true;
  264.                 }
  265.  
  266.                 // Add enclosed string
  267.                 $line .= $this->_enclosure $element $this->_enclosure;
  268.             }
  269.  
  270.             // Add line ending
  271.             $line .= $this->_lineEnding;
  272.  
  273.             // Write to file
  274.             fwrite($pFileHandle$line);
  275.         else {
  276.             throw new Exception("Invalid parameters passed.");
  277.         }
  278.     }
  279.  
  280.     /**
  281.      * Get Pre-Calculate Formulas
  282.      *
  283.      * @return boolean 
  284.      */
  285.     public function getPreCalculateFormulas({
  286.         return $this->_preCalculateFormulas;
  287.     }
  288.  
  289.     /**
  290.      * Set Pre-Calculate Formulas
  291.      *
  292.      * @param boolean $pValue    Pre-Calculate Formulas?
  293.      * @return PHPExcel_Writer_CSV 
  294.      */
  295.     public function setPreCalculateFormulas($pValue true{
  296.         $this->_preCalculateFormulas $pValue;
  297.         return $this;
  298.     }
  299. }

Documentation generated on Tue, 01 Jun 2010 17:02:40 +0200 by phpDocumentor 1.4.3