PDA

View Full Version : having problems with psxlsgen (a php xls generator class)



zur
10-15-2007, 01:29 PM
hi,

i'm using a class called psxlsgen, which can be found on it's sourceforge site here -> http://sourceforge.net/projects/psxlsgen/

there is such a problem that the columns D and E always turn out to be 700px, there must be a way to fix that..?
another issue is that calculations in the cells i'm printing using php (like (A2+B4) or whatever) won't be calculated, but stay as they are, i guess that's because of the distinction of numbers and text..
the first one is a bigger issue for me at the moment, and i'd appreciate it very much if anyone could help me...

the php code of the inserted file is like this:
<?php
if( !defined( "PHP_SIMPLE_XLS_GEN" ) ) {
define( "PHP_SIMPLE_XLS_GEN", 1 );

class PhpSimpleXlsGen {
var $xls_data = ""; // where generated xls be stored
var $default_dir = ""; // default directory to be saved file
var $filename = "xxx"; // save filename
var $fname = ""; // filename with full path
var $crow = 0; // current row number
var $ccol = 0; // current column number
var $totalcol = 0; // total number of columns
var $get_type = 0; // 0=stream, 1=file
var $errno = 0; // 0=no error
var $error = ""; // error string
var $dirsep = "/"; // directory separator
var $header = 1; // 0=no header, 1=header line for xls table

// Default constructor
function PhpSimpleXlsGen()
{
$os = getenv( "OS" );
$temp = getenv( "TEMP");
// check OS and set proper values for some vars.
if ( stristr( $os, "Windows" ) ) {
$this->default_dir = $temp;
$this->dirsep = "\\";
} else {
// assume that is Unix/Linux
$this->default_dir = "/tmp";
$this->dirsep = "/";
}
// begin of the excel file header
$this->xls_data = pack( "ssssss", 0x809, 0x08, 0x00,0x10, 0x0, 0x0 );
// check header text
if ( $this->header ) {
$this->Header();
}
}

function Header( $text="" ) {
if ( $text == "" ) {
$text1 = "xxx";
$text2 = date("D, d M Y H:i:s T");
}
if ( $this->totalcol < 1 ) {
$this->totalcol = 1;
}
$this->InsertText( $text1 );
$this->InsertText( $text2 );
$this->crow += 2;
$this->ccol = 0;
}

// end of the excel file
function End()
{
$this->xls_data .= pack("sssssssC", 0x7D, 11, 3, 4, 25600,0,0,0);
$this->xls_data .= pack( "ss", 0x0A, 0x00 );
return;
}

// write a Number (double) into row, col
function WriteNumber_pos( $row, $col, $value ) {
$this->xls_data .= pack( "sssss", 0x0203, 14, $row, $col, 0x00 );
$this->xls_data .= pack( "d", $value );
return;
}
// write a label (text) into Row, Col
function WriteText_pos( $row, $col, $value ) {
$len = strlen( $value );
$this->xls_data .= pack( "s*", 0x0204, 8 + $len, $row, $col, 0x00, $len );
$this->xls_data .= $value;
return;
}

// insert a number, increment row,col automatically
function InsertNumber( $value )
{
if ( $this->ccol == $this->totalcol ) {
$this->ccol = 0;
$this->crow++;
}
$this->WriteNumber_pos( $this->crow, $this->ccol, &$value );
$this->ccol++;
return;
}

// insert a number, increment row,col automatically
function InsertText( $value )
{
if ( $this->ccol == $this->totalcol ) {
$this->ccol = 0;
$this->crow++;
}
$this->WriteText_pos( $this->crow, $this->ccol, &$value );
$this->ccol++;
return;
}

// Change position of row,col
function ChangePos( $newrow, $newcol )
{
$this->crow = $newrow;
$this->ccol = $newcol;
return;
}

// new line
function NewLine()
{
$this->ccol = 0;
$this->crow++;
return;
}

// send generated xls as stream file
/*function SendFile( $filename )
{
$this->filename = $filename;
$this->SendFile();
}*/
// send generated xls as stream file
function SendFile($filename = "") {
if ($filename != "") $this->filename= $filename;
$this->End();
header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
header ( "Pragma: no-cache" );
header ( "Content-type: application/x-msexcel" );
header ( "Content-Disposition: attachment; filename=$this->filename.xls" );
header ( "Content-Description: PHP Generated XLS Data" );
print $this->xls_data;
}

// change the default saving directory
function ChangeDefaultDir( $newdir )
{
$this->default_dir = $newdir;
return;
}

// Save generated xls file
/*function SaveFile( $filename )
{
$this->filename = $filename;
$this->SaveFile();
}
*/

// Save generated xls file
function SaveFile($filename = "")
{
if ($filename != "") $this->filename=$filename;
$this->End();
$this->fname = $this->default_dir."$this->dirsep".$this->filename;
if ( !stristr( $this->fname, ".xls" ) ) {
$this->fname .= ".xls";
}
$fp = fopen( $this->fname, "wb" );
fwrite( $fp, $this->xls_data );
fclose( $fp );
return;
}

// Get generated xls as specified type
function GetXls( $type = 0 ) {
if ( !$type && !$this->get_type ) {
$this->SendFile();
} else {
$this->SaveFile();
}
}
} // end of the class PHP_SIMPLE_XLS_GEN
}
// end of ifdef PHP_SIMPLE_XLS_GEN



and the code where it is used will be something like this (from the example of the class):

include( "psxlsgen.php" );

$myxls = new PhpSimpleXlsGen();
$myxls->totalcol = 2;
$myxls->InsertText( "Erol" );
$myxls->InsertText( "Ozcan" );
$myxls->ChangePos(7,0);
$myxls->InsertText( "You must pay" );
$myxls->InsertNumber( 20.48 );
$myxls->WriteText_pos(4,2, "USD to use this class :-))" ); // hidden costs :-))
$myxls->SendFile();