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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.5, 2009-01-05
  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 - 2009 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.      * Whether to write a BOM (for UTF8).
  94.      *
  95.      * @var boolean 
  96.      */
  97.     private $_useBOM = false;
  98.  
  99.     /**
  100.      * Create a new PHPExcel_Writer_CSV
  101.      *
  102.      * @param     PHPExcel    $phpExcel    PHPExcel object
  103.      */
  104.     public function __construct(PHPExcel $phpExcel{
  105.         $this->_phpExcel     = $phpExcel;
  106.         $this->_delimiter     = ',';
  107.         $this->_enclosure     = '"';
  108.         $this->_lineEnding     = PHP_EOL;
  109.         $this->_sheetIndex     = 0;
  110.     }
  111.  
  112.     /**
  113.      * Save PHPExcel to file
  114.      *
  115.      * @param     string         $pFileName 
  116.      * @throws     Exception
  117.      */
  118.     public function save($pFilename null{
  119.         // Fetch sheet
  120.         $sheet $this->_phpExcel->getSheet($this->_sheetIndex);
  121.  
  122.         $saveArrayReturnType PHPExcel_Calculation::getArrayReturnType();
  123.         PHPExcel_Calculation::setArrayReturnType(PHPExcel_Calculation::RETURN_ARRAY_AS_VALUE);
  124.  
  125.         // Open file
  126.         $fileHandle fopen($pFilename'w');
  127.         if ($fileHandle === false{
  128.             throw new Exception("Could not open file $pFilename for writing.");
  129.         }
  130.  
  131.         if ($this->_useBOM{
  132.             // Write the UTF-8 BOM code
  133.             fwrite($fileHandle"\xEF\xBB\xBF");
  134.         }
  135.  
  136.         // Convert sheet to array
  137.         $cellsArray $sheet->toArray(''$this->_preCalculateFormulas);
  138.  
  139.         // Write rows to file
  140.         foreach ($cellsArray as $row{
  141.             $this->_writeLine($fileHandle$row);
  142.         }
  143.  
  144.         // Close file
  145.         fclose($fileHandle);
  146.  
  147.         PHPExcel_Calculation::setArrayReturnType($saveArrayReturnType);
  148.     }
  149.  
  150.     /**
  151.      * Get delimiter
  152.      *
  153.      * @return string 
  154.      */
  155.     public function getDelimiter({
  156.         return $this->_delimiter;
  157.     }
  158.  
  159.     /**
  160.      * Set delimiter
  161.      *
  162.      * @param    string    $pValue        Delimiter, defaults to ,
  163.      */
  164.     public function setDelimiter($pValue ','{
  165.         $this->_delimiter = $pValue;
  166.     }
  167.  
  168.     /**
  169.      * Get enclosure
  170.      *
  171.      * @return string 
  172.      */
  173.     public function getEnclosure({
  174.         return $this->_enclosure;
  175.     }
  176.  
  177.     /**
  178.      * Set enclosure
  179.      *
  180.      * @param    string    $pValue        Enclosure, defaults to "
  181.      */
  182.     public function setEnclosure($pValue '"'{
  183.         if ($pValue == ''{
  184.             $pValue null;
  185.         }
  186.         $this->_enclosure = $pValue;
  187.     }
  188.  
  189.     /**
  190.      * Get line ending
  191.      *
  192.      * @return string 
  193.      */
  194.     public function getLineEnding({
  195.         return $this->_lineEnding;
  196.     }
  197.  
  198.     /**
  199.      * Set line ending
  200.      *
  201.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  202.      */
  203.     public function setLineEnding($pValue PHP_EOL{
  204.         $this->_lineEnding = $pValue;
  205.     }
  206.  
  207.     /**
  208.      * Get whether BOM should be used
  209.      *
  210.      * @return boolean 
  211.      */
  212.     public function getUseBOM({
  213.         return $this->_useBOM;
  214.     }
  215.  
  216.     /**
  217.      * Set whether BOM should be used
  218.      *
  219.      * @param    boolean    $pValue        Use UTF-8 byte-order mark? Defaults to false
  220.      */
  221.     public function setUseBOM($pValue false{
  222.         $this->_useBOM = $pValue;
  223.     }
  224.  
  225.     /**
  226.      * Get sheet index
  227.      *
  228.      * @return int 
  229.      */
  230.     public function getSheetIndex({
  231.         return $this->_sheetIndex;
  232.     }
  233.  
  234.     /**
  235.      * Set sheet index
  236.      *
  237.      * @param    int        $pValue        Sheet index
  238.      */
  239.     public function setSheetIndex($pValue 0{
  240.         $this->_sheetIndex = $pValue;
  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.      */
  297.     public function setPreCalculateFormulas($pValue true{
  298.         $this->_preCalculateFormulas = $pValue;
  299.     }
  300. }

Documentation generated on Mon, 05 Jan 2009 20:36:45 +0100 by phpDocumentor 1.4.1