Jump to content

Exporting to Excel problem


radi8

Recommended Posts

Hey all:

The following code sections comes from the psxlsgen.php class available on Sourceforge. This class is no longer updated by the author so I am asking the question here. The functions provided are NOT the entire class, rather the primary functions used,

 

I have this working nicely when I use the mysql output to Excel, but I am trying to get it to work in a different way. I have a form that generates data to the user and have an option to create the Excel file from the data. All seems to work (creating the Excel temp file, inserting data, etc...) but, when I view the Excel file, it contains the image of the web page and i cannot for the life of me understand why.

 

Can anyone glean anything from this and help?

 

I am using the following code to create and export data into an excel file:

// instantiate the excel temp file
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();
       }
     }
// inserts data into the file
function InsertText( $value )
     {
        $doText=true;
        if (is_numeric($value)) 
        {
            if (substr($value, 0, 1) == "0" && $value != 0 && substr($value, 0, 2) !== "0."){
                $this->InsertNumber($value);
                $doText=false;
            }
            else{
                $this->InsertNumber($value);
                $doText=false;
            } 
        }
        if($doText)
        {
            if ( $this->ccol == $this->totalcol ) 
            {
                $this->ccol = 0;
                $this->crow++;
            }
            $this->WriteText_pos( $this->crow, $this->ccol, &$value );
            $this->ccol++;
        }
        return;
     }
// sends the file
function SendFile()
     {
        $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;
     }


Link to comment
https://forums.phpfreaks.com/topic/181928-exporting-to-excel-problem/
Share on other sites

I'm not familiar with the psxlsgen class, and I'd recommend you switch to use a library that is still actively supported.

However, I'm intrigued by this line:

$this->WriteText_pos( $this->crow, $this->ccol, &$value );

What version of PHP are you using? Or is the psxlsgen class using? because passing by reference in this manner is deprecated.

I believe that the version on my BSD box is 4.3.4, and this lib was created in 2001 or something.

 

I have been using it for mysql dumps and it works nicely, but was trying to use it in a different manner.

 

No worries, I will get a newer lib and try again. The big issue is that most Excel output streams are based on PEAR which we do not hahp 4 server).

 

I am thinking the issue in in the Header() declarations. Thanks anyways for the help, it was a shot inthe dark.

I'd recommend the PHPExcel library listed in my sig, but you'd need to upgrade PHP on your server as well... although I'd recommend that too PHP4 really is very old now)

 

I know, I have been trying to get my sysadmin to install a new BSD box with PHP 5.2 but he is seriously dragging his feet on it. So many problems can be resolved with this version of PHP... oh well, will keep trudging along until he does.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.