+++ /dev/null
-<?php // $Id$\r
-/****************************************************************\r
-* Script : PHP Simple Excel File Generator - Base Class\r
-* Project : PHP SimpleXlsGen\r
-* Author : Erol Ozcan <eozcan@superonline.com>\r
-* Version : 0.3\r
-* Copyright : GNU LGPL\r
-* URL : http://psxlsgen.sourceforge.net\r
-* Last modified : 13 Jun 2001\r
-* Description : This class is used to generate very simple\r
-* MS Excel file (xls) via PHP.\r
-* The generated xls file can be obtained by web as a stream\r
-* file or can be written under $default_dir path. This package\r
-* is also included mysql, pgsql, oci8 database interaction to\r
-* generate xls files.\r
-* Limitations:\r
-* - Max character size of a text(label) cell is 255\r
-* ( due to MS Excel 5.0 Binary File Format definition )\r
-*\r
-* Credits : This class is based on Christian Novak's small\r
-* Excel library functions.\r
-******************************************************************/\r
-\r
-if( !defined( "PHP_SIMPLE_XLS_GEN" ) ) {\r
- define( "PHP_SIMPLE_XLS_GEN", 1 );\r
-\r
- class PhpSimpleXlsGen {\r
- var $class_ver = "0.3"; // class version\r
- var $xls_data = ""; // where generated xls be stored\r
- var $default_dir = ""; // default directory to be saved file\r
- var $filename = "psxlsgen"; // save filename\r
- var $fname = ""; // filename with full path\r
- var $crow = 0; // current row number\r
- var $ccol = 0; // current column number\r
- var $totalcol = 0; // total number of columns\r
- var $get_type = 0; // 0=stream, 1=file\r
- var $errno = 0; // 0=no error\r
- var $error = ""; // error string\r
- var $dirsep = "/"; // directory separator\r
- var $header = 1; // 0=no header, 1=header line for xls table\r
-\r
- // Default constructor\r
- function PhpSimpleXlsGen()\r
- {\r
- $os = getenv( "OS" );\r
- $temp = getenv( "TEMP");\r
- // check OS and set proper values for some vars.\r
- if ( stristr( $os, "Windows" ) ) {\r
- $this->default_dir = $temp;\r
- $this->dirsep = "\\";\r
- } else {\r
- // assume that is Unix/Linux\r
- $this->default_dir = "/tmp";\r
- $this->dirsep = "/";\r
- }\r
- // begin of the excel file header\r
- $this->xls_data = pack( "vvvvvv", 0x809, 0x08, 0x00,0x10, 0x0, 0x0 );\r
- // check header text\r
- if ( $this->header ) {\r
- $this->Header();\r
- }\r
- }\r
-\r
- function Header( $text="" ) {\r
- if ( $text == "" ) {\r
- $text = "This file was generated using PSXlsGen at ".date("D, d M Y H:i:s T");\r
- }\r
- if ( $this->totalcol < 1 ) {\r
- $this->totalcol = 1;\r
- }\r
- $this->InsertText( $text );\r
- $this->crow += 2;\r
- $this->ccol = 0;\r
- }\r
-\r
- // end of the excel file\r
- function End()\r
- {\r
- $this->xls_data .= pack("vvvvvvvC", 0x7D, 11, 3, 4, 25600,0,0,0);\r
- $this->xls_data .= pack( "vv", 0x0A, 0x00 );\r
- return;\r
- }\r
-\r
- // write a Number (double) into row, col\r
- function WriteNumber_pos( $row, $col, $value )\r
- {\r
- $this->xls_data .= pack( "vvvvv", 0x0203, 14, $row, $col, 0x00 );\r
- $this->xls_data .= pack( "d", $value );\r
- return;\r
- }\r
-\r
- // write a label (text) into Row, Col\r
- function WriteText_pos( $row, $col, $value )\r
- {\r
- $len = strlen( $value );\r
- $this->xls_data .= pack( "v*", 0x0204, 8 + $len, $row, $col, 0x00, $len );\r
- $this->xls_data .= $value;\r
- return;\r
- }\r
-\r
- // insert a number, increment row,col automatically\r
- function InsertNumber( $value )\r
- {\r
- if ( $this->ccol == $this->totalcol ) {\r
- $this->ccol = 0;\r
- $this->crow++;\r
- }\r
- $this->WriteNumber_pos( $this->crow, $this->ccol, $value );\r
- $this->ccol++;\r
- return;\r
- }\r
-\r
- // insert text, increment row,col automatically\r
- function InsertText( $value )\r
- {\r
- if ( $this->ccol == $this->totalcol ) {\r
- $this->ccol = 0;\r
- $this->crow++;\r
- }\r
- $this->WriteText_pos( $this->crow, $this->ccol, $value );\r
- $this->ccol++;\r
- return;\r
- }\r
-\r
- // Change position of row,col\r
- function ChangePos( $newrow, $newcol )\r
- {\r
- $this->crow = $newrow;\r
- $this->ccol = $newcol;\r
- return;\r
- }\r
-\r
- // new line\r
- function NewLine()\r
- {\r
- $this->ccol = 0;\r
- $this->crow++;\r
- return;\r
- }\r
-\r
- // send generated xls as stream file\r
- function SendFileName( $filename )\r
- {\r
- $this->filename = $filename;\r
- $this->SendFile();\r
- }\r
- // send generated xls as stream file\r
- function SendFile()\r
- {\r
- global $HTTP_USER_AGENT;\r
-\r
- $this->End();\r
- header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );\r
- header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );\r
- header ( "Pragma: no-cache" );\r
- if (!strstr($HTTP_USER_AGENT,"MSIE")) {\r
- $attachment=" attachment;";\r
- } else {\r
- $attachment="";\r
- }\r
- header("Content-Type: application/x-msexcel\r\n");\r
- header("Content-Disposition:$attachment filename=$this->filename.xls\r\n\r\n");\r
- header("Content-Transfer-Encoding: binary\r\n"); \r
- header("Content-Description: Excel Spreadsheet" );\r
- print $this->xls_data;\r
- }\r
-\r
- // change the default saving directory\r
- function ChangeDefaultDir( $newdir )\r
- {\r
- $this->default_dir = $newdir;\r
- return;\r
- }\r
-\r
- // Save generated xls file\r
- function SaveFileName( $filename )\r
- {\r
- $this->filename = $filename;\r
- $this->SaveFile();\r
- }\r
-\r
- // Save generated xls file\r
- function SaveFile()\r
- {\r
- $this->End();\r
- $this->fname = $this->default_dir."$this->dirsep".$this->filename;\r
- if ( !stristr( $this->fname, ".xls" ) ) {\r
- $this->fname .= ".xls";\r
- }\r
- $fp = fopen( $this->fname, "wb" );\r
- fwrite( $fp, $this->xls_data );\r
- fclose( $fp );\r
- return;\r
- }\r
-\r
- // Get generated xls as specified type\r
- function GetXls( $type = 0 ) {\r
- if ( !$type && !$this->get_type ) {\r
- $this->SendFile();\r
- } else {\r
- $this->SaveFile();\r
- }\r
- }\r
- } // end of the class PHP_SIMPLE_XLS_GEN\r
-}\r
-// end of ifdef PHP_SIMPLE_XLS_GEN\r