Jump to content

PHPExcel -- Issue sending to browser


arenaninja

Recommended Posts

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 1 year later...

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.