Sep
18
2010
Генератор CSV-файла на PHP

CSV-файлы очень популярны для представления электронных таблиц - файл является текстовым, ячейки таблицы в нём разделяются точкой с запятой. Представляем вашему вниманию класс, разработанный Кондраковым Александром Владимировичем, позволяющий генерировать CSV-файл из массива PHP.

Генерация CSV-файла


<?php

class CsvReader



    private 
$file;

    private 
$delimiter

    private 
$length;

    private 
$handle

    private 
$csvArray

    

    public function 
__construct($file$delimiter=";"$length 8000

    { 

       
$this->file $file

       
$this->length $length

       
$this->delimiter $delimiter

       
$this->FileOpen(); 

    } 

    public function 
__destruct() 

    { 

       
$this->FileClose(); 

    } 

    public function 
GetCsv()

    {

        
$this->SetCsv();

        if(
is_array($this->csvArray)) 

         return 
$this->csvArray;

    }

    

    private function 
SetCsv()

    {

        if(
$this->GetSize())

        {

            while ((
$data = @fgetcsv($this->handle$this->length$this->delimiter)) !== FALSE)

            {

                
$this->csvArray[] = $data;

            }

        }

    }

    private function 
FileOpen()

    {

        
$this->handle=($this->IsFile())?fopen($this->file'r'):null;

    }

    private function 
FileClose()

    {

        if(
$this->handle

         @
fclose($this->handle); 

    }

    private function 
GetSize()

    {

        if(
$this->IsFile())

            return (
filesize($this->file));

        else

            return 
false;

    }

    private function 
IsFile()

    {

        if(
is_file($this->file) && file_exists($this->file))

            return 
true;

        else

            return 
false;

    }





class 
CsvWriter

{

    private 
$file;

    private 
$delimiter;

    private 
$array;

    private 
$handle;

    public function 
__construct($file$array$delimiter=";")

    {

        
$this->file $file

        
$this->array $array

        
$this->delimiter $delimiter;

        
$this->FileOpen();

    }

    public function 
__destruct()

    {

        
$this->FileClose();

    }

    public function 
GetCsv()

    {

        
$this->SetCsv();

    }

    

    private function 
IsWritable()

    {

        if(
is_writable($this->file))

            return 
true;

        else

            return 
false;

    }

    private function 
SetCsv() 

    { 

      if(
$this->IsWritable())

      {

          
$content ""

          foreach(
$this->array as $ar

          { 

             
$content .= implode($this->delimiter$ar);

             
$content .= "\r\n"

          } 

          if (
fwrite($this->handle$content) === FALSE

                 exit;

      }

    }

    private function 
FileOpen()

    {

        
$this->handle=fopen($this->file'w+');

    }

    private function 
FileClose()

    {

        if(
$this->handle

         @
fclose($this->handle); 

    } 

}

$array = array(array('1','1','1'), array('2','2','2'), array('3','3','3'));

$dd = new CsvWriter('test.txt',$array);

$dd->GetCsv();

?> 


 

Комментарии
Добавить новый Поиск
Оставить комментарий
Имя:
Email:
 
Веб-сайт:
Тема:
UBB-Код:
[b] [i] [u] [url] [quote] [code] [img] 
 
 
:angry::0:confused::cheer:B):evil::silly::dry::lol::kiss::D:pinch:
:(:shock::X:side::):P:unsure::woohoo::huh::whistle:;):s
:!::?::idea::arrow:
 
Пожалуйста, введите проверочный код, который Вы видите на картинке.
Powered by !JoomlaComment 3.26

3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

 

Кто онлайн

Сейчас 51 гостей онлайн
Чем оригинальные Автозапчасти www.motor-lux.com.