Jump to content


Photo

PHPExcel -- Issue sending to browser


  • Please log in to reply
4 replies to this topic

#1 arenaninja

arenaninja

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 06 July 2012 - 06:53 PM

Hey all. I just installed PHPExcel and I'm trying to do some tests. I'm trying to create my very first workbook with it, I'm doing this:
        if(file_exists('../sys/lib/phpexcel/PHPExcel.php'))
        {
            require_once '../sys/lib/phpexcel/PHPExcel.php';
        }
        else
            die("PHPExcel not found!");
        $phpExcel = new PHPExcel();
        // set metadata
        $phpExcel->getProperties()
                ->setCreator($_SESSION['firstname']." ".$_SESSION['lastname'])
                ->setLastModifiedBy($_SESSION['firstname']." ".$_SESSION['lastname'])
                ->setTitle("Test document")
                ->setSubject("Some Report")
                ->setDescription("Testing testing");

        // set headers to redirect output to client browser as a file download
        header('Content-Type: application/vnd.openXMLformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="myfile.xlsx"');
        header('Cache-Control: max-age=0');
        //-----Create a Writer and output the file to the browser-----
        $objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
        $objWriter->save('php://output');  //push out to the client browser
        $phpExcel->disconnectWorksheets();
        unset($phpExcel);
Which is in line with just about every example that I've seen. However, what I get instead is that the entire webpage is sent to Excel, along with this gibberish prepended (shown at the very top):
PKᄄト?GメDᄇX?[Content_Types].xmlᆳヤMN?ナ??%nY トレvAa ユ(0?ؖg?{&i ノ@?bE??y?d۸l m?ムチ?X﾿(??﾿メ?ᄍ?;@1_ヘ?リᄆ?)jᄁx/%?ナEネ?ᆭ ᄅQĿi!ᆪ?ᄉy3?<チᄃワZ1ᄑ0?Y?%zV c?Ib7?ヌ?a/l٥P1:ᆱq??jᆰ?0Aᆵヨン??"??(フ ヤ?タW?マ?Tj?{ܲ??O??X٭??B�ニ~׭?ツ?ᄂ6?=ᄀoBZᄒヌᄚᄐt?X4?ᄒCg?ナネメQgmrL?ٯc??t?Z?ノヌᄡᄏhPv???u?ユᄂ>ᆪj -' V?ヌ#メ?F^6?n?ᄡ8q"Kノ??>_
What gives? My webpage is utf-8 encoded (as is everything I'm working on)> I've verified that the properties are NOT being set (I have a suspicion that this is what that gibberish is).

Any advice is helpful at this point. Thanks.

#2 kicken

kicken

    Wiser? Not exactly.

  • Gurus
  • 2,651 posts
  • LocationBonita, FL

Posted 06 July 2012 - 07:34 PM

However, what I get instead is that the entire webpage is sent to Excel, along with this gibberish prepended (shown at the very top):


If you want to output an Excel document, you cannot output anything else such as HTML for a page.  You can only send one document to the browser per request so you can send either a HTML page or the Excel file, but not both.

Is this stuff showing in the browser window or is it actually opening excel and showing it in the excel window?

If the former, then you probably have output before your header() calls which means the headers indicating the excel content are ignored (you should see a warning unless you have error reporting turned down/off).

If the latter, possibly you have extra junk after the excel document (your html page maybe) so excel is opening the file as a text file?  I would have expected it to complain about the file being invalid instead but never tried such a thing so not sure how it'd react.

Recycle your old CD's, don't trash them!
Did I help you out?  Feeling generous? I accept tips via Paypal or Bitcoin @ 14mDxaob8Jgdg52scDbvf3uaeR61tB2yC7

#3 arenaninja

arenaninja

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 06 July 2012 - 07:48 PM

kicken,

This stuff is opening Excel and showing it in the excel window. There's nothing else before or after this code. The user clicks a button on the page that makes a call for this function. Before trying this I have been using this to create an Excel report:
        $filename = 'recharge-report.xls';
        $report = "<html>
    <body>";
        $report.=$this->_reporttable;
        $report.= "</body>
<html>";
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=$filename");
        echo $report;

It works, but I'm looking to fancify all of this (mostly automate sub-totals to remove burden from MySQL and please the accounting monkeys).

I also suspect either php_xml or php_gd2 may not be enabled. My server is a CentOS server running PHP 5.3.10. I've looked all over a page with phpinfo() for php_xml and php_gd2, and I can find this (which relates to gd2):
gd
GD Support 
enabled 

GD Version 
bundled (2.0.34 compatible)
But I can't find anything about php_xml, and I've spent a few minutes in google trying to figure out how to install it but I haven't found anything. I've also perused the php.ini file and have found no reference to either gd2 or php_xml.

In short, I still need a ton of help  :shrug:
PS: I don't have root access to the server, but the IT Manager is extremely accomodating and he will almost certainly install any missing plugins. However I'd like to know what/how to ask for to mask my ignorance ;)

#4 arenaninja

arenaninja

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 06 July 2012 - 08:37 PM

Alright the weirdness continues. I've managed to set values to the cells inside the workbook but the header content isn't showing (and weird squiggly signs are gone). My guess is that it may have something to do with using LibreOffice to open .xlsx files? I'm gonna test it from my laptop in a few hours.

#5 mylove9558

mylove9558

    Newbie

  • New Members
  • Pip
  • 1 posts

Posted 29 April 2014 - 09:34 PM

you can edit code:

 

//-----Create a Writer and output the file to the browser-----
        $objWriter = PHPExcel_IOFactory::createWriter($phpExcel, 'Excel2007');
        $objWriter->save('php://output');  //push out to the client browser
       // $phpExcel->disconnectWorksheets(); ==>Rem this code
        //unset($phpExcel);==>Rem this code






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com