Excel xls helper

This article is also available in the following languages:
By wasenbr
Helper to creation of simple xls files.
This the helper code:

Helper Class:

<?php 
/**
 * By Cleiton Wasen
 * wasenbr at gmail.com
 * Based in http://www.appservnetwork.com/modules.php?name=News&file=article&sid=8
 *  
 */
class XlsHelper {
    
    var 
$helpers = array();
    
    
/**
     * set the header configuration
     * @param $filename the xls file name
     */
    
function setHeader($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 ");
    }
    
    
/**
     * write the xls begin of file
     */
    
function BOF() {
        echo 
pack("ssssss"0x8090x80x00x100x00x0);  
        return;
    }
    
    
/**
     * write the xls end of file
     */
    
function EOF() {
        echo 
pack("ss"0x0A0x00);
        return;
    }
    
    
/**
     * write a number
     * @param $Row row to write $Value (first row is 0)
     * @param $Col column to write $Value (first column is 0)
     * @param $Value number value
     */
    
function writeNumber($Row$Col$Value) {
        echo 
pack("sssss"0x20314$Row$Col0x0);
        echo 
pack("d"$Value);
        return;
    }
    
    
/**
     * write a string label
     * @param $Row row to write $Value (first row is 0)
     * @param $Col column to write $Value (first column is 0)
     * @param $Value string value
     */
    
function writeLabel($Row$Col$Value) {
        
$L strlen($Value);
        echo 
pack("ssssss"0x204$L$Row$Col0x0$L);
        echo 
$Value;
        return;
    }

}
?>

View example:

View Template:


<?php
    
// Send Header
    
$xls->setHeader('text_'.date('Y_m_d').'.xls');

    
// XLS Data Cell
    
$xls->BOF();
    
$xls->writeLabel(1,0,"Student Register");
    
$xls->writeLabel(2,0,"COURSENO : ");
    
$xls->writeLabel(2,1,"123");
    
$xls->writeLabel(3,0,"TITLE : ");
    
$xls->writeLabel(3,0,"BlaBlaBla");
    
$xls->writeLabel(4,0,"SETION : ");
    
$xls->writeLabel(6,0,"NO");
    
$xls->writeLabel(6,1,"ID");
    
$xls->writeLabel(6,2,"Gender");
    
$xls->writeLabel(6,3,"Name");
    
$xls->writeLabel(6,4,"Lastname");
    
$xls->EOF();
    exit();
?>

Comments

  • Posted 11/23/10 09:54:39 PM
    This helper work as said, but i have a strange problem, when the excel opens, i encounter an error.

    "File Error: Data May Be Lost" (How do i get rid of this)

    I clicked "ok", the data will display correctly, any idea why?

    I am using safari on mac.
    • Posted 05/19/11 12:14:03 AM
      [quote] This helper work as said, but i have a strange problem, when the excel opens, i encounter an error.

      "File Error: Data May Be Lost" (How do i get rid of this)

      I clicked "ok", the data will display correctly, any idea why?

      I am using safari on mac.
      [end quote] I found that I was overwriting data. Make sure you are traversing through your rows and columns correctly. I realize that post was forever ago, but for future viewers.
    • Posted 03/08/11 06:48:19 PM
      [quote] This helper work as said, but i have a strange problem, when the excel opens, i encounter an error.

      Hi pekklegup,

      Did you manage to figure out how to fix this?

      Farez

      "File Error: Data May Be Lost" (How do i get rid of this)

      I clicked "ok", the data will display correctly, any idea why?

      I am using safari on mac.
      [end quote]
      • Posted 04/28/11 02:00:36 PM
        [quote] [quote] This helper work as said, but i have a strange problem, when the excel opens, i encounter an error.

        Hi pekklegup,

        Did you manage to figure out how to fix this?

        Farez

        "File Error: Data May Be Lost" (How do i get rid of this)

        I clicked "ok", the data will display correctly, any idea why?

        I am using safari on mac.
        [end quote] [end quote]
        The data can be a different cell, ie, currently it is overriding the cell. Should be:
        $xls->writeLabel(3,0,"TITLE : ");
        $xls->writeLabel(3,1,"BlaBlaBla");

        Regards!
  • Posted 07/02/10 02:43:07 PM
    Easier than others, worked fine at first attempt.
    Very Good.

Comments are closed for articles over a year old