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.3, 2008-08-25
  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.         // Get cell collection
  122.         $cellCollection = $sheet->getCellCollection();
  123.         
  124.         // Get column count
  125.         $colCount = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn());
  126.         
  127.         // Loop trough cells
  128.         $currentRow = -1;
  129.         $rowData = array();
  130.         foreach ($cellCollection as $cell) {                    
  131.             if ($currentRow != $cell->getRow()) {
  132.                 // End previous row?
  133.                 if ($currentRow != -1) {
  134.                     $this->_writeLine($fileHandle, $rowData);
  135.                 }
  136.  
  137.                 // Set current row
  138.                 $currentRow = $cell->getRow();
  139.             
  140.                 // Start a new row
  141.                 $rowData = array();
  142.                 for ($i = 0; $i < $colCount; $i++) {
  143.                     $rowData[$i] = '';
  144.                 }
  145.             }
  146.                     
  147.             // Copy cell
  148.             $column = PHPExcel_Cell::columnIndexFromString($cell->getColumn()) - 1;
  149.             if ($cell->getValue() instanceof PHPExcel_RichText) {
  150.                 $rowData[$column] = $cell->getValue()->getPlainText();
  151.             } else {
  152.                 if ($this->_preCalculateFormulas) {
  153.                     $rowData[$column] = PHPExcel_Style_NumberFormat::toFormattedString(
  154.                         $cell->getCalculatedValue(),
  155.                         $sheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  156.                     );
  157.                 } else {
  158.                     $rowData[$column] = PHPExcel_Style_NumberFormat::toFormattedString(
  159.                         $cell->getValue(),
  160.                         $sheet->getstyle( $cell->getCoordinate() )->getNumberFormat()->getFormatCode()
  161.                     );
  162.                 }
  163.             }
  164.         }
  165.         
  166.         // End last row?
  167.         if ($currentRow != -1) {
  168.             $this->_writeLine($fileHandle, $rowData);
  169.         }
  170.                 
  171.         // Close file
  172.         fclose($fileHandle);
  173.     }
  174.     
  175.     /**
  176.      * Get delimiter
  177.      * 
  178.      * @return string 
  179.      */
  180.     public function getDelimiter() {
  181.         return $this->_delimiter;
  182.     }
  183.     
  184.     /**
  185.      * Set delimiter
  186.      * 
  187.      * @param    string    $pValue        Delimiter, defaults to ,
  188.      */
  189.     public function setDelimiter($pValue = ',') {
  190.         $this->_delimiter = $pValue;
  191.     }
  192.     
  193.     /**
  194.      * Get enclosure
  195.      * 
  196.      * @return string 
  197.      */
  198.     public function getEnclosure() {
  199.         return $this->_enclosure;
  200.     }
  201.     
  202.     /**
  203.      * Set enclosure
  204.      * 
  205.      * @param    string    $pValue        Enclosure, defaults to "
  206.      */
  207.     public function setEnclosure($pValue = '"') {
  208.         if ($pValue == '') {
  209.             $pValue = null;
  210.         }
  211.         $this->_enclosure = $pValue;
  212.     }
  213.     
  214.     /**
  215.      * Get line ending
  216.      * 
  217.      * @return string 
  218.      */
  219.     public function getLineEnding() {
  220.         return $this->_lineEnding;
  221.     }
  222.     
  223.     /**
  224.      * Set line ending
  225.      * 
  226.      * @param    string    $pValue        Line ending, defaults to OS line ending (PHP_EOL)
  227.      */
  228.     public function setLineEnding($pValue = PHP_EOL) {
  229.         $this->_lineEnding = $pValue;
  230.     }
  231.     
  232.     /**
  233.      * Get sheet index
  234.      * 
  235.      * @return int 
  236.      */
  237.     public function getSheetIndex() {
  238.         return $this->_sheetIndex;
  239.     }
  240.     
  241.     /**
  242.      * Set sheet index
  243.      * 
  244.      * @param    int        $pValue        Sheet index
  245.      */
  246.     public function setSheetIndex($pValue = 0) {
  247.         $this->_sheetIndex = $pValue;
  248.     }
  249.     
  250.     /**
  251.      * Write line to CSV file
  252.      * 
  253.      * @param    mixed    $pFileHandle    PHP filehandle
  254.      * @param    array    $pValues        Array containing values in a row
  255.      * @throws    Exception
  256.      */
  257.     private function _writeLine($pFileHandle = null, $pValues = null) {
  258.         if (!is_null($pFileHandle) && is_array($pValues)) {
  259.             // No leading delimiter
  260.             $writeDelimiter = false;
  261.             
  262.             // Build the line
  263.             $line = '';
  264.             
  265.             foreach ($pValues as $element) {
  266.                 // Decode UTF8 data
  267.                 if (PHPExcel_Shared_String::IsUTF8($element)) {
  268.                     $element = utf8_decode($element);
  269.                 }
  270.                 
  271.                 // Escape enclosures
  272.                 $element = str_replace($this->_enclosure, $this->_enclosure . $this->_enclosure, $element);
  273.  
  274.                 // Add delimiter
  275.                 if ($writeDelimiter) {
  276.                     $line .= $this->_delimiter;
  277.                 } else {
  278.                     $writeDelimiter = true;
  279.                 }
  280.                 
  281.                 // Add enclosed string
  282.                 $line .= $this->_enclosure . $element . $this->_enclosure;
  283.             }
  284.             
  285.             // Add line ending
  286.             $line .= $this->_lineEnding;
  287.             
  288.             // Write to file
  289.             fwrite($pFileHandle, $line);
  290.         } else {
  291.             throw new Exception("Invalid parameters passed.");
  292.         }
  293.     }
  294.  
  295.     /**
  296.      * Get Pre-Calculate Formulas
  297.      *
  298.      * @return boolean 
  299.      */
  300.     public function getPreCalculateFormulas() {
  301.         return $this->_preCalculateFormulas;
  302.     }
  303.     
  304.     /**
  305.      * Set Pre-Calculate Formulas
  306.      *
  307.      * @param boolean $pValue    Pre-Calculate Formulas?
  308.      */
  309.     public function setPreCalculateFormulas($pValue = true) {
  310.         $this->_preCalculateFormulas = $pValue;
  311.     }
  312. }

Documentation generated on Mon, 25 Aug 2008 13:34:16 +0200 by phpDocumentor 1.4.1