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

Documentation generated on Mon, 11 Jan 2010 08:07:35 +0100 by phpDocumentor 1.4.1