Artigos & Ideias

Ler Folha Excel com PHP (Parte II)

Escrito por:

Ainda como foi falado no post anterior em como Ler Folha Excel com PHP, aqui fica um update.

Questões de performance aparte, esta é uma solução simples, basta trocar o PDO por uma classe de leitura do excel e adaptarmos. Considerando que tornamos a aplicação apenas para leitura.

Há algum tempo encontrei uma classe muito interessante, que como não sei quem é o autor, fica aqui o primeiro link que encontrei: OLEREAD, que é parte de um projecto maior, ou pelo menos aparenta ser.

Entretanto fica aqui também o código:

< ?php
define('NUM_BIG_BLOCK_DEPOT_BLOCKS_POS', 0x2c);
define('SMALL_BLOCK_DEPOT_BLOCK_POS', 0x3c);
define('ROOT_START_BLOCK_POS', 0x30);
define('BIG_BLOCK_SIZE', 0x200);
define('SMALL_BLOCK_SIZE', 0x40);
define('EXTENSION_BLOCK_POS', 0x44);
define('NUM_EXTENSION_BLOCK_POS', 0x48);
define('PROPERTY_STORAGE_BLOCK_SIZE', 0x80);
define('BIG_BLOCK_DEPOT_BLOCKS_POS', 0x4c);
define('SMALL_BLOCK_THRESHOLD', 0x1000);
// property storage offsets
define('SIZE_OF_NAME_POS', 0x40);
define('TYPE_POS', 0x42);
define('START_BLOCK_POS', 0x74);
define('SIZE_POS', 0x78);
define('IDENTIFIER_OLE', pack("CCCCCCCC",0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1));

//echo 'ROOT_START_BLOCK_POS = '.ROOT_START_BLOCK_POS."\n";

//echo bin2hex($data[ROOT_START_BLOCK_POS])."\n";
//echo "a=";
//echo $data[ROOT_START_BLOCK_POS];
//function log

function GetInt4d($data, $pos) {
        return ord($data[$pos]) | (ord($data[$pos+1]) << icon cool   Ler Folha Excel com PHP (Parte II) | (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24);
}

class OLERead {
    var $data = '';

    function OLERead(){

    }

    function read($sFileName){

    	// check if file exist and is readable (Darko Miljanovic)
    	if(!is_readable($sFileName)) {
    		$this->error = 1;
    		return false;
    	}

    	$this->data = @file_get_contents($sFileName);
    	if (!$this->data) {
    		$this->error = 1;
    		return false;
   		}
   		//echo IDENTIFIER_OLE;
   		//echo 'start';
   		if (substr($this->data, 0, icon cool   Ler Folha Excel com PHP (Parte II) != IDENTIFIER_OLE) {
    		$this->error = 1;
    		return false;
   		}
        $this->numBigBlockDepotBlocks = GetInt4d($this->data, NUM_BIG_BLOCK_DEPOT_BLOCKS_POS);
        $this->sbdStartBlock = GetInt4d($this->data, SMALL_BLOCK_DEPOT_BLOCK_POS);
        $this->rootStartBlock = GetInt4d($this->data, ROOT_START_BLOCK_POS);
        $this->extensionBlock = GetInt4d($this->data, EXTENSION_BLOCK_POS);
        $this->numExtensionBlocks = GetInt4d($this->data, NUM_EXTENSION_BLOCK_POS);

	/*
        echo $this->numBigBlockDepotBlocks." ";
        echo $this->sbdStartBlock." ";
        echo $this->rootStartBlock." ";
        echo $this->extensionBlock." ";
        echo $this->numExtensionBlocks." ";
        */
        //echo "sbdStartBlock = $this->sbdStartBlock\n";
        $bigBlockDepotBlocks = array();
        $pos = BIG_BLOCK_DEPOT_BLOCKS_POS;
       // echo "pos = $pos";
	$bbdBlocks = $this->numBigBlockDepotBlocks;

            if ($this->numExtensionBlocks != 0) {
                $bbdBlocks = (BIG_BLOCK_SIZE - BIG_BLOCK_DEPOT_BLOCKS_POS)/4;
            }

        for ($i = 0; $i < $bbdBlocks; $i++) {
              $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
              $pos += 4;
        }

        for ($j = 0; $j < $this->numExtensionBlocks; $j++) {
            $pos = ($this->extensionBlock + 1) * BIG_BLOCK_SIZE;
            $blocksToRead = min($this->numBigBlockDepotBlocks - $bbdBlocks, BIG_BLOCK_SIZE / 4 - 1);

            for ($i = $bbdBlocks; $i < $bbdBlocks + $blocksToRead; $i++) {
                $bigBlockDepotBlocks[$i] = GetInt4d($this->data, $pos);
                $pos += 4;
            }

            $bbdBlocks += $blocksToRead;
            if ($bbdBlocks < $this->numBigBlockDepotBlocks) {
                $this->extensionBlock = GetInt4d($this->data, $pos);
            }
        }

       // var_dump($bigBlockDepotBlocks);

        // readBigBlockDepot
        $pos = 0;
        $index = 0;
        $this->bigBlockChain = array();

        for ($i = 0; $i < $this->numBigBlockDepotBlocks; $i++) {
            $pos = ($bigBlockDepotBlocks[$i] + 1) * BIG_BLOCK_SIZE;
            //echo "pos = $pos";
            for ($j = 0 ; $j < BIG_BLOCK_SIZE / 4; $j++) {
                $this->bigBlockChain[$index] = GetInt4d($this->data, $pos);
                $pos += 4 ;
                $index++;
            }
        }

	//var_dump($this->bigBlockChain);
        //echo '=====2';
        // readSmallBlockDepot();
        $pos = 0;
	    $index = 0;
	    $sbdBlock = $this->sbdStartBlock;
	    $this->smallBlockChain = array();

	    while ($sbdBlock != -2) {

	      $pos = ($sbdBlock + 1) * BIG_BLOCK_SIZE;

	      for ($j = 0; $j < BIG_BLOCK_SIZE / 4; $j++) {
	        $this->smallBlockChain[$index] = GetInt4d($this->data, $pos);
	        $pos += 4;
	        $index++;
	      }

	      $sbdBlock = $this->bigBlockChain[$sbdBlock];
	    }

        // readData(rootStartBlock)
        $block = $this->rootStartBlock;
        $pos = 0;
        $this->entry = $this->__readData($block);

        /*
        while ($block != -2)  {
            $pos = ($block + 1) * BIG_BLOCK_SIZE;
            $this->entry = $this->entry.substr($this->data, $pos, BIG_BLOCK_SIZE);
            $block = $this->bigBlockChain[$block];
        }
        */
        //echo '==='.$this->entry."===";
        $this->__readPropertySets();

    }

     function __readData($bl) {
        $block = $bl;
        $pos = 0;
        $data = '';

        while ($block != -2)  {
            $pos = ($block + 1) * BIG_BLOCK_SIZE;
            $data = $data.substr($this->data, $pos, BIG_BLOCK_SIZE);
            //echo "pos = $pos data=$data\n";
	    $block = $this->bigBlockChain[$block];
        }
		return $data;
     }

    function __readPropertySets(){
        $offset = 0;
        //var_dump($this->entry);
        while ($offset < strlen($this->entry)) {
              $d = substr($this->entry, $offset, PROPERTY_STORAGE_BLOCK_SIZE);

              $nameSize = ord($d[SIZE_OF_NAME_POS]) | (ord($d[SIZE_OF_NAME_POS+1]) < < 8);

              $type = ord($d[TYPE_POS]);
              //$maxBlock = strlen($d) / BIG_BLOCK_SIZE - 1;

              $startBlock = GetInt4d($d, START_BLOCK_POS);
              $size = GetInt4d($d, SIZE_POS);

            $name = '';
            for ($i = 0; $i < $nameSize ; $i++) {
              $name .= $d[$i];
            }

            $name = str_replace("\x00", "", $name);

            $this->props[] = array (
                'name' => $name,
                'type' => $type,
                'startBlock' => $startBlock,
                'size' => $size);

            if (($name == "Workbook") || ($name == "Book")) {
                $this->wrkbook = count($this->props) - 1;
            }

            if ($name == "Root Entry") {
                $this->rootentry = count($this->props) - 1;
            }

            //echo "name ==$name=\n";

            $offset += PROPERTY_STORAGE_BLOCK_SIZE;
        }

    }

    function getWorkBook(){
    	if ($this->props[$this->wrkbook]['size'] < SMALL_BLOCK_THRESHOLD){
//    	  getSmallBlockStream(PropertyStorage ps)

			$rootdata = $this->__readData($this->props[$this->rootentry]['startBlock']);

			$streamData = '';
	        $block = $this->props[$this->wrkbook]['startBlock'];
	        //$count = 0;
	        $pos = 0;
		    while ($block != -2) {
      	          $pos = $block * SMALL_BLOCK_SIZE;
		          $streamData .= substr($rootdata, $pos, SMALL_BLOCK_SIZE);

			      $block = $this->smallBlockChain[$block];
		    }

		    return $streamData;

    	}else{

	        $numBlocks = $this->props[$this->wrkbook]['size'] / BIG_BLOCK_SIZE;
	        if ($this->props[$this->wrkbook]['size'] % BIG_BLOCK_SIZE != 0) {
	            $numBlocks++;
	        }

	        if ($numBlocks == 0) return '';

	        //echo "numBlocks = $numBlocks\n";
	    //byte[] streamData = new byte[numBlocks * BIG_BLOCK_SIZE];
	        //print_r($this->wrkbook);
	        $streamData = '';
	        $block = $this->props[$this->wrkbook]['startBlock'];
	        //$count = 0;
	        $pos = 0;
	        //echo "block = $block";
	        while ($block != -2) {
	          $pos = ($block + 1) * BIG_BLOCK_SIZE;
	          $streamData .= substr($this->data, $pos, BIG_BLOCK_SIZE);
	          $block = $this->bigBlockChain[$block];
	        }
	        //echo 'stream'.$streamData;
	        return $streamData;
    	}
    }

}
?>

Faz o download dessa classe e comecemos o trabalho sujo, como ler um excel:

Primeiro, para iniciar a leitura do ficheiro fazemos:

< ?
include 'oleread.php' ;
$file = "ficheiro.xls" ;
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read($file);
?>

Com isto, a estrutura da folha XLS será carregada no Objecto $data. Para acedermos a uma folha precisamos aceder à propriedade “sheets”, um array com todas as folhas do ficheiro (cada “aba” na parte de baixo do excel) pelo número da ordem em que se encontram começando do 0. Exemplo de como obtemos a primeira folha do ficheiro excel:

< ? $first = $data->sheets[0] ; ?>

Ou podemos ainda percorrer todas as folhas:

< ? foreach($data->sheets as $sheet) { // código aqui } ?>

Cada folha (sheet) possui uma propriedade chamada ‘cells’, que é um array multidimensional com todas as células da folha, até aonde foi preenchida. O primeiro índice do array é a linha, o segundo a coluna. Eis um exemplo:

< ? $cells = $data->sheets[0]['cells'] ;
echo "nTitulo da Folha (linha 1, coluna A):". $cells[1][1] ;
echo "nSubtitulo (linha 2, coluna C):".$cells[2][3] ;
?>

Não tem grandes mistérios, é só não nos perdermos entre tantas listas icon wink   Ler Folha Excel com PHP (Parte II)

Questões, feel free to ask!!

3


Sobre o Autor

Carlos Gonçalves é um Freelancer com bastante experiência em linguagens de programação e webdesign. Vocacionado para as novas tecnologias, aposta na qualidade dos serviços, atendimento rápido e sempre disponível para melhor satisfazer os seus clientes. Autor do site cgoncalves.com

Comentários

Comentar

Nota: A moderação de comentários está activada e pode atrasar o teu comentário. Não há necessidade de reenviares o teu comentário.

  1. Carlos Gonçalves  Abril 19, 2010
  2. Carlos Gonçalves  Abril 19, 2010
  3. Tweets that mention Ler Folha Excel com PHP | Carlos Gonçalves -- Topsy.com  Abril 22, 2010
More in Outros (1 of 12 articles)

# #