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.4, 2010-08-26
  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.         $saveDebugLog PHPExcel_Calculation::getInstance()->writeDebugLog;
  110.         PHPExcel_Calculation::getInstance()->writeDebugLog false;
  111.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  112.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  113.  
  114.         // Open file
  115.         $fileHandle fopen($pFilename'w');
  116.         if ($fileHandle === false{
  117.             throw new Exception("Could not open file $pFilename for writing.");
  118.         }
  119.  
  120.         if ($this->_useBOM{
  121.             // Write the UTF-8 BOM code
  122.             fwrite($fileHandle"\xEF\xBB\xBF");
  123.         }
  124.  
  125.         // Convert sheet to array
  126.         $cellsArray $sheet->toArray(''$this->_preCalculateFormulas);
  127.  
  128.         // Write rows to file
  129.         foreach ($cellsArray as $row{
  130.             $this->_writeLine($fileHandle$row);
  131.         }
  132.  
  133.         // Close file
  134.         fclose($fileHandle);
  135.  
  136.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  137.         PHPExcel_Calculation::getInstance()->writeDebugLog $saveDebugLog;
  138.     }
  139.  
  140.     /**
  141.      * Get delimiter
  142.      *
  143.      * @return string 
  144.      */
  145.     public function getDelimiter({
  146.         return $this->_delimiter;
  147.     }
  148.  
  149.     /**
  150.      * Set delimiter
  151.      *
  152.      * @param    string    $pValue        Delimiter, defaults to ,
  153.      * @return PHPExcel_Writer_CSV 
  154.      */
  155.     public function setDelimiter($pValue ','{
  156.         $this->_delimiter $pValue;
  157.         return $this;
  158.     }
  159.  
  160.     /**
  161.      * Get enclosure
  162.      *
  163.      * @return string 
  164.      */
  165.     public function getEnclosure({
  166.         return $this->_enclosure;
  167.     }
  168.  
  169.     /**
  170.      * Set enclosure
  171.      *
  172.      * @param    string    $pValue        Enclosure, defaults to "
  173.      * @return PHPExcel_Writer_CSV 
  174.      */
  175.     public function setEnclosure($pValue '"'{
  176.         if ($pValue == ''{
  177.             $pValue null;
  178.         }
  179.         $this->_enclosure $pValue;
  180.         return $this;
  181.     }
  182.  
  183.     /**
  184.      * Get line ending
  185.      *
  186.      * @return string 
  187.      */
  188.     public function getLineEnding({
  189.         return $this->_lineEnding;
  190.     }
  191.  
  192.     /**
  193.      * Set line ending
  194.      *
  195.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  196.      * @return PHPExcel_Writer_CSV 
  197.      */
  198.     public function setLineEnding($pValue PHP_EOL{
  199.         $this->_lineEnding $pValue;
  200.         return $this;
  201.     }
  202.  
  203.     /**
  204.      * Get whether BOM should be used
  205.      *
  206.      * @return boolean 
  207.      */
  208.     public function getUseBOM({
  209.         return $this->_useBOM;
  210.     }
  211.  
  212.     /**
  213.      * Set whether BOM should be used
  214.      *
  215.      * @param    boolean    $pValue        Use UTF-8 byte-order mark? Defaults to false
  216.      * @return PHPExcel_Writer_CSV 
  217.      */
  218.     public function setUseBOM($pValue false{
  219.         $this->_useBOM $pValue;
  220.         return $this;
  221.     }
  222.  
  223.     /**
  224.      * Get sheet index
  225.      *
  226.      * @return int 
  227.      */
  228.     public function getSheetIndex({
  229.         return $this->_sheetIndex;
  230.     }
  231.  
  232.     /**
  233.      * Set sheet index
  234.      *
  235.      * @param    int        $pValue        Sheet index
  236.      * @return PHPExcel_Writer_CSV 
  237.      */
  238.     public function setSheetIndex($pValue 0{
  239.         $this->_sheetIndex $pValue;
  240.         return $this;
  241.     }
  242.  
  243.     /**
  244.      * Write line to CSV file
  245.      *
  246.      * @param    mixed    $pFileHandle    PHP filehandle
  247.      * @param    array    $pValues        Array containing values in a row
  248.      * @throws    Exception
  249.      */
  250.     private function _writeLine($pFileHandle null$pValues null{
  251.         if (!is_null($pFileHandle&& is_array($pValues)) {
  252.             // No leading delimiter
  253.             $writeDelimiter false;
  254.  
  255.             // Build the line
  256.             $line '';
  257.  
  258.             foreach ($pValues as $element{
  259.                 // Escape enclosures
  260.                 $element str_replace($this->_enclosure$this->_enclosure $this->_enclosure$element);
  261.  
  262.                 // Add delimiter
  263.                 if ($writeDelimiter{
  264.                     $line .= $this->_delimiter;
  265.                 else {
  266.                     $writeDelimiter true;
  267.                 }
  268.  
  269.                 // Add enclosed string
  270.                 $line .= $this->_enclosure $element $this->_enclosure;
  271.             }
  272.  
  273.             // Add line ending
  274.             $line .= $this->_lineEnding;
  275.  
  276.             // Write to file
  277.             fwrite($pFileHandle$line);
  278.         else {
  279.             throw new Exception("Invalid parameters passed.");
  280.         }
  281.     }
  282.  
  283.     /**
  284.      * Get Pre-Calculate Formulas
  285.      *
  286.      * @return boolean 
  287.      */
  288.     public function getPreCalculateFormulas({
  289.         return $this->_preCalculateFormulas;
  290.     }
  291.  
  292.     /**
  293.      * Set Pre-Calculate Formulas
  294.      *
  295.      * @param boolean $pValue    Pre-Calculate Formulas?
  296.      * @return PHPExcel_Writer_CSV 
  297.      */
  298.     public function setPreCalculateFormulas($pValue true{
  299.         $this->_preCalculateFormulas $pValue;
  300.         return $this;
  301.     }
  302. }

Documentation generated on Thu, 26 Aug 2010 17:41:05 +0200 by phpDocumentor 1.4.3