Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/11/2008, 09:18
jorgivaz
 
Fecha de Ingreso: junio-2005
Mensajes: 343
Antigüedad: 18 años, 10 meses
Puntos: 10
[APORTE] Exportación a Excel

Hola, aquí les dejo una libreria que he "completado" con algunas cosas.

Cuando la vi me gusto bastante, así que aquí lo tienen para PHP 5, para quien le pueda ser de utilidad.

Código PHP:
/*
 * EJEMPLO DE USO
 */

    
$xls = new Excel();
    
$xls->addRow(Array("Nombre","Apellidos","Website","ID"));
    
$xls->addRow(Array("nombre1","apellidos1","www.pagina1.com",1));
    
$xls->addRow(Array("nombre2","apellidos2","www.pagina2.com",2));    
    
$arr = Array(
            Array(
"nombre3","apellidos3","www.pagina3.com","3"),
            Array(
"nombre4","apellidos4","www.pagina4.com","4"),
            Array(
"nombre5","apellidos5","www.pagina5.com","5")
    );
    
$xls->addTable($arr);
    
$xls->download("pagina.xls");
    
/*
 * CLASS
 */
class Excel {

    private 
$file;
    private 
$row;

    
/*
     * Constructor
     */
    
function __construct(){ 
        
$this->file $this->__BOF();
        
$row 0;
    }
    
    
/*
     * Inicio del fichero
     */
    
private function __BOF() {
        return 
pack("ssssss"0x8090x80x00x100x00x0);
    }
    
    
/*
     * Final del fichero
     */
    
private function __EOF() {
        return 
pack("ss"0x0A0x00);
    }
    
    
/*
     * Escribe un número en una fila y columna
     */
    
private function __writeNum($row$col$value) {
        
$this->file .= pack("sssss"0x20314$row$col0x0);
        
$this->file .= pack("d"$value);
    }
    
    
/*
     * Escribe un string en una fila y columna
     */
    
private function __writeString($row$col$value ) {
        
$L strlen($value);
        
$this->file .= pack("ssssss"0x204$L$row$col0x0$L);
        
$this->file .= $value;
    }
    
    
/*
     * Escribe un valor en una fila y columna, este método decide si será un número o un string.
     */
    
private function writeCell($value,$row,$col) {
        if(
is_numeric($value)) {
            
$this->__writeNum($row,$col,$value);
        }elseif(
is_string($value)) {
            
$this->__writeString($row,$col,$value);
        }
    }
    
    
/*
     * Añadir datos de una fila
     */
    
public function addRow($data,$row=null) {
        
$columns count($data);
        
        if(!isset(
$row)) {
            
$row $this->row;
            
$this->row++;
        }
        for(
$i=0$i<$columns$i++) {
            
$cell $data[$i];
            
$this->writeCell($cell,$row,$i);
        }
    }
    
    
/*
     * Añadir datos de una tabla
     */
    
public function addTable($data) {
        
$rows count($data);
        
        for(
$j=0;$j<$rows;$j++){
        
            
$row $this->row;
            
$this->row++;
            
            
$columns count($data[$j]);
            
            for(
$i=0$i<$columns$i++) {
        
                
$cell $data[$j][$i];
                
$this->writeCell($cell,$row,$i);
        
            }
        }
    }    
    
    
/* 
     * Genera un fichero para descargar en memoria
     */
    
public function download($filename) {
        
header("Pragma: public");
        
header("Expires: 0");
        
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
header("Content-Type: application/force-download");
        
header("Content-Type: application/octet-stream");
        
header("Content-Type: application/download");;
        
header("Content-Disposition: attachment;filename=$filename ");
        
header("Content-Transfer-Encoding: binary ");
        
$this->write();
    }
    
    
/*
     * Escribe el contenido del fichero
     */
    
private function write() {
        echo 
$file $this->file.$this->__EOF();
    }
    

El fuente original es de fecha de marzo del 2007:
http://chumby.net/2007/03/27/php-excel-export-class/

Edito para añadir también una exportación diferente, se trata de exportar a excel directamente el código HTML, ya que el excel (a no ser que sea muy antiguo) lo sabe interpretar.

Código PHP:
    $xls = new ExcelHTML();
    
$xls->addRow(Array("Nombre","Apellidos","Website","ID"));
    
$xls->addRow(Array("nombre1","apellidos1","www.pagina1.com",1));
    
$xls->addRow(Array("nombre2","apellidos2","www.pagina2.com",2));    
    
$arr = Array(
            Array(
"nombre3","apellidos3","www.pagina3.com","3"),
            Array(
"nombre4","apellidos4","www.pagina4.com","4"),
            Array(
"nombre5","apellidos5","www.pagina5.com","5")
    );
    
$xls->addTable($arr);
    
$xls->download("pagina.xls");


class 
ExcelHTML {

    private 
$file;
    
/*
     * Constructor
     */
    
function __construct(){ 
        
$this->file $this->__BOF();
        
$row 0;
    }
    
    
/*
     * Inicio del fichero
     */
    
private function __BOF() {
        return 
"<table>";
    }
    
    
/*
     * Final del fichero
     */
    
private function __EOF() {
        return 
"</table>";
    }
    
    
/*
     * Escribe un valor.
     */
    
private function writeCell($value) {
        
$this->file .= "<td>$value</td>";
    }
    
    
/*
     * Escribe el inicio/final de una fila
     */
    
private function writeRow($s){
        if (
$s==0$this->file .= "<tr>";
        if (
$s==1$this->file .= "</tr>";
    }
    
/*
     * Añadir datos de una fila
     */
    
public function addRow($data) {
        
$columns count($data);
        
$this->writeRow(0);
        for(
$i=0$i<$columns$i++) {
            
$cell $data[$i];
            
$this->writeCell($cell);
        }
        
$this->writeRow(1);
    }
    
    
/*
     * Añadir datos de una tabla
     */
    
public function addTable($data) {
        
$rows count($data);
        for(
$j=0;$j<$rows;$j++){
            
$this->addRow($data[$j]);
        }
    }    
    
    
/* 
     * Genera un fichero para descargar en memoria
     */
    
public function download($filename) {
        
header("Pragma: public");
        
header("Expires: 0");
        
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
header("Content-Type: application/force-download");
        
header("Content-Type: application/octet-stream");
        
header("Content-Type: application/download");;
        
header("Content-Disposition: attachment;filename=$filename ");
        
header("Content-Transfer-Encoding: binary ");
        
$this->write();
    }
    
    
/*
     * Escribe el contenido del fichero
     */
    
private function write() {
        echo 
$file $this->file.$this->__EOF();
    }
    

Exportación a CSV

Código PHP:

 $xls 
= new ExcelCSV("\r\n");
    
$xls->addRow(Array("Nombre","Apellidos","Website","ID"));
    
$xls->addRow(Array("nombre1","apellidos1","www.pagina1.com",1));
    
$xls->addRow(Array("nombre2","apellidos2","www.pagina2.com",2));    
    
$arr = Array(
            Array(
"nombre3","apellidos3","www.pagina3.com","3"),
            Array(
"nombre4","apellidos4","www.pagina4.com","4"),
            Array(
"nombre5","apellidos5","www.pagina5.com","5")
    );
    
$xls->addTable($arr);
    
$xls->download("pagina.csv");


class 
ExcelCSV {

    private 
$file;
    private 
$crlf
    
    
// "\n" -> Linux
    // "\r\n" -> Windows
    
    /*
     * Constructor
     */
    
function __construct($c){ 
        
$this->crlf=$c;
    }
    
    
/*
     * Escribe un valor.
     */
    
private function writeCell($value) {
        
$this->file .= "$value;";
    }
    
    
/*
     * Escribe el inicio/final de una fila
     */
    
private function writeRow(){
        
$this->file substr($this->file,0,strlen($this->file)-1).$this->crlf;
    }
    
/*
     * Añadir datos de una fila
     */
    
public function addRow($data) {
        
$columns count($data);
        for(
$i=0$i<$columns$i++) {
            
$cell $data[$i];
            
$this->writeCell($cell);
        }
        
$this->writeRow();
    }
    
    
/*
     * Añadir datos de una tabla
     */
    
public function addTable($data) {
        
$rows count($data);
        for(
$j=0;$j<$rows;$j++){
            
$this->addRow($data[$j]);
        }
    }    
    
    
/* 
     * Genera un fichero para descargar en memoria
     */
    
public function download($filename) {
        
header("Pragma: public");
        
header("Expires: 0");
        
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        
header("Content-Type: application/force-download");
        
header("Content-Type: application/octet-stream");
        
header("Content-Type: application/download");;
        
header("Content-Disposition: attachment;filename=$filename ");
        
header("Content-Transfer-Encoding: binary ");
        
$this->write();
    }
    
    
/*
     * Escribe el contenido del fichero
     */
    
private function write() {
        echo 
$file $this->file;
    }
    


Última edición por jorgivaz; 04/11/2008 a las 09:54 Razón: Añado otra clase para exportar directamente el HTML a Excel y exportación también a CSV