arenaninja Posted July 6, 2012 Share Posted July 6, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265334-phpexcel-issue-sending-to-browser/ Share on other sites More sharing options...
kicken Posted July 7, 2012 Share Posted July 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265334-phpexcel-issue-sending-to-browser/#findComment-1359791 Share on other sites More sharing options...
arenaninja Posted July 7, 2012 Author Share Posted July 7, 2012 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/265334-phpexcel-issue-sending-to-browser/#findComment-1359794 Share on other sites More sharing options...
arenaninja Posted July 7, 2012 Author Share Posted July 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/265334-phpexcel-issue-sending-to-browser/#findComment-1359804 Share on other sites More sharing options...
mylove9558 Posted April 30, 2014 Share Posted April 30, 2014 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 Quote Link to comment https://forums.phpfreaks.com/topic/265334-phpexcel-issue-sending-to-browser/#findComment-1477669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.