title: Forcing PHPExcel download in CodeIgniter doesn't work
body: <p>I'm trying to download a excelsheet that has been generated with PHPExcel, being new to PHPExcel i've been trying to do this with the provided Example in the documentation. However I can't get the script to download for me, the only thing it does is outputting the following;</p>
  
  <pre><code>    PKjGGD²Xð[Content_Types].xml­MNÃ0÷"ò%nY vAa (0ö¤±êØgúw{&amp;i@ÕnbEö{ßøyìÑdÛ¸l
      mð¥×ÁX¿(ÅÛü)¿òF¹à¡;@1_æ»±Øc)j¢x/%êEày¦
  ©QÄ¿i!£ÒKµy3ÜJ&lt;§Z1½0?YÙL%zV
  cäÖIb7Øûìa/lÙ¥P1:«qáríÍjªÊj0A¯Íuë""íàÙ( ÁWìMä)Tjå({Ü²û&gt;óOãÂ,XÙ­ÁÚÆ&gt;Bÿ~×­ÏõÓ¤6ÜJ=¡oBZ¾°¼tìíX4Êú¾Cgñ,QgmrLÙÙ¯cïeëàtø± Zõ?´»hPvÃðÂ±úÿuÕjÀ¼Râæ¸øûî}¬CvÖøPKjG¶78éK_rels/.rels­ÍjÃ0ï{
  £{£´1F^Ê ·2²Ðlå$±½-}ûy±ºÒÃ¥OvûyÕ;ØÓ°.JPìØÞµ^ê§Õ¨ÈYÅ±3GØWw»g)åØõ&gt;ªqQCD¦ãb!]þi$Lò3´èÉÔ2nÊòÃoT¦:Z
  áh· ê³ç[ØÒ4½á·]ºÐyNì,Û¹&gt;¤&gt;£j
  -'
  VÌ)#÷EF^6ÚÜnô÷´8q"KÐHàë&gt;_×Öÿ¹¢eÆÍ&lt;âáUdøvÁÅ
  TPKjG$±Vé¹xl/_rels/workbook.xml.rels­ÁjÃ0Dïý
  ±÷ZvZJ)s)\[÷´¶LlIh7mý÷UH¡Ä¬ØÇHëÍ÷8OLÔ¯ *JèM°½ï|4ÛûgÄÚ[=
  &amp;$ØÔwë74çr}$M&lt;)pÌñEJ2GMEèóMÒ¨9ËÔÉ¨Í^w(Weù$ÓÜêO±³
  ÒÎV )â¼CÛö_9èùJ$Ì/:dG]d×ãWKÆsÞÅsú&lt;«[VàtBûÎ)?ð¼ùøÌã0_!íÉ!òä4úEÍÇ©yñãêPKjG3È8docProps/app.xmlSÁnÛ0½ï+Ý9Á0¢¢H7ô°bvgV¦c¡²$¬ìë';°ë¬;M§ÇÇçgR·§Ö&amp;²ÁoÄrQ½   õÇx:|¿ù*
  bð¸àq#ÎHâVR»"&amp;¶HEvð´
  s\KI¦ÁhÛ&gt;wêZà\¦£um
  ÞóÖ¢g¹*Ë/O¾Âê&amp;Nââ¸îøM«`ú|ô|8Çì§Õ]ÎàüúÑ(Ô\|;tJÎ*íÑ¼%Ëg]*9/ÕÞÃm6Ö58B%ß    õÐÏl6V¯;4RAöwÚJ/@ØÇÙÏâ"»v8é_!½RÈ¤äDp®cûY/A×B9Éø:âÁ²CúYï ñ?/çbqoÛèðC¾ñKyoCÁgßCF®äØVàá½Á~XÿJOñîqû5©ö
  $¬òMM×2ê!çO®×oðG¬FÍÇF¿$Ï «EÏ°#§äûÎë?PKjGòqå1µ¢docProps/core.xml¥AoÛ Çïûëæ`­,ÇºªÚ¡Ñ"5Óreðâ°b@çúÛcÇM×*íÞïýòú©Qè8/^cºÈ1Íº^ã»»ìF&gt;0-2Ö¸¯«w%·7¶ÎXpAGQ¤}ÁíC°!¡a~   ãâÒÕÄ2þÈj Ë&lt;¿"
  &amp;X`dfv6âI)ø¬´­SI 8
  èà  ]PòÌp¿¸!UÎÈFÞÂEôTé'/g°ëºE·Jhìýæþ!I=DÅW¥àwÀqÕ1@£¦dmW³â¤b&gt;lbä â¦¿À¿fÊiÐÑÅqSåçêëíîWË~Ê(Íh¾£WÅòc±¢ïsZäùÐÇÉ³µNú?íÉÒ2(¨¾¹}Fûû=ÚèÖðv¸ËÌ[xî¤
  ñiVâ[Bÿª&gt; 4¸FëãûEÛo[Äcpü"©ÏÃ¾ýõxxKW't¸¯Gè;ã¯ÌÙ¾é#{´éÂf(}ØUm\?áÀ·*!$t®«ßªúPKjGs{Y³¦xl/theme/theme1.xmlíYOoÛ6¿ïSº·²lÉu:EìØëÖ¦
  ·C´DK¬)Q é¤¾
  íqÀaÝ°ËÝv¶h]ºO­ÃÖý
  {úcé6iSlCë-¿÷ïñQ¾|å^ÌÐ!ò¤k9"Ï]ëÖhx¡c!©p`ÆÒµæDZW¶&gt;¸7UDb&lt;¸kEJ¥¶-}Æò"OIk.b¬`(B;øØÆÌn6m;Æ4±Pcàzs2¡&gt;A£¥µµ`&gt;`ð(MøLø¹D"ÇS'ûsÙgbÖµ@NÀFä²ÃRÁB×jäËÞºl/ZC«Ñ
  óOIWÓfN'ÂñÐºvüÿUÜ`0è%¿},uV°î°ãô&lt;5Pñ¸Ê»ßðn¯ño­à7z½·QÃ·*¼»ï4Úîv³w+¼·ªo»ßo×ð^o¯à6Ún"Fé
  :ç22KÈ³«FxàÅ¨P¶¶»
  úD­Ûk1¾ËÅyp±¢   RóL°¸&gt;ÇâLÞ$X[)¦|¹2ÉBÒ4U]ëãCFTO|ñô1zñôÑñý'Ç÷9~ðàøþÏÂ«8   uÂçßñ÷·¢¿÷üáWf¼Ôñ¿ÿôÙo¿~i*øìëG&lt;yôìÏÿüá¡¾-ðXhL$ºAÐ&gt;Á62g£EÖ(pHp ¢ðÆ3®GêÎ»- ÎîÖt=ÄLQðZ×»³Fs®e²tsfIh.f:nãCìþÐf)ìdjbÙHMÍ=ÑÆ!IBÙb »CiÍ¯»Ô\òBw(êajtÉè*!.sluÍ7»·Q3ûrXGBB`fbIXÍâÂ±Qc3y«È¤äÁ\ø5K  ãh)M47Å¼¦î5Èö]6ëH¡èÔ¼9×;|Úpu¦I¤c?SØ¢íqeT×3$Cp²6Ü·)QgKë[4Ì$[ SJ^ÏÇ9`õ½V©c¼¬l3
  uû}Ù^À·ác§(ÖëpÿÃ½gÉ¬x_¡ßWèw±B¯Ëåó¯ËU)¶õ^;g¯m¼'±5gäºÌ¸ó!LæhÙç§&lt;âj¸Pàü    ®&gt;¡*:p
  b\B(KÖ¡D)p»°ÖòÎ¯¨lÎç¼Å½ÐXíò né÷Í%|J]P+cpZa­Ko&amp;Ì)§æxfiÞK¥Ù7!oÎ^&amp;8íf!6
  f$Èü^0XåÜC$#2FÑ§uJ·u^í5MÚFëÍ¤&amp;Hº8w8ï¢ÔX½,©Ðhå5=ù8íZè¹à1NÌJfaÒµ|UòÊd&gt;i°y[:µ×D¤Bª,£*_Z¼I*ýùá|°_WVÇùµ°OL&amp;ÄWkfªa¹Æg(8Bc6ûôvÝP   GEs1¡n¹ñê_fÁÉ×&gt;ev`F¸¬I-ö&lt;^ê4õì5º¿¦)­s4Å{wMÉv.4¸­ ¿zA 0Êöh×âBEªPQ( qÈe^Ò"S   ±ìv¦+9¬êVÁ£(ra¤öiJ§"AÈ*í|3§©¯FeYª+ÓâwL  eÙÛÎì·P´¨&amp;¥#rÜÉ Ù¦ìÃÿpçã6^§=¨¹géE\­èkGÁÆ©pÆ£¶i¶¸éú¨Má²/(ÜTø¬êoG|¢%x¡S¦ßrr:w4ã2Vo·ªBÐi¼ýæSsvk³·ãlÏàkïå®¶WSÔÖ.2ùhåÏ,&gt;¾²wà~4cJïîÁ¥´¿øøØéÖ?PKjG·ì£Óxl/sharedStrings.xml]ÎAj1Ð}Oájßñ$Pí,¡h buÆ`ËK¦·¯K,ÿû|øîp+Ù\©Iªìa3`Ï5&amp;&lt;|~^ÞÀ"GÌÉÃ ÂQÓ§,fÕeo­g*(C]{óU[Aí±MVFe&amp;Òívw¶`b0+§ËJÇº²zxà$§ár®Îjpöþñ»¶ïdû«ðPKjGâ_Þùh
  xl/styles.xmlËn0÷}
  ËûÃRµ5ºhT5TêÖÀV|Aöp
  yúÚÃE6Ìü&gt;?ûQ
  rc¹V)oNªtÍUÒ?Eþù+%ªÐ
  R:¥÷Ù§Äâ$à¹@âw¶g[í
  X0 ^T6¥bE¶ê@2{£{Pn¥ÑF2t©i#ëaµõ$)¢óéô%+%j¹DK*=(LéFYÒhµ)1
  BäÂÄRÂw±EÃ_`'!Vî]æõ×Ç±Ï*-´!¦-Sç§ùò²brA&lt;2ÁKÃ½çump!Ö¾Î4YÒ3D0*w    Yâbêak 
  uT·MñùÖcÛÇ}áò ò¸°uí¨ë~óÍµ]jS»o¿74HþÁeÑùB&lt;ûïý·9
      ßèGíí%ÞkèøK0!ñØ=-°ßbÇæ-ò¸Õû|Âú^L¹ç%dO,ÁäóÛÕ8À=Ì-ÿ.x«$L°kJ:mø«ÃûÓÐÃõã¼òR©µ¤aÄßÎSå¨ÿë'¦´aÂºù°áê¥Ð9_5oÎØ,­æÌVüYUâOfJü«¹6VÓÊäêË³ãH!+Ý$]jhØ °X¥nñO¨ù ¿ÑkÕ/~Ñ¸TÍñV:S´ý#²ÿPKjG´´!Ðxl/workbook.xmlQËNA¼ç+Fs» @å5R $Hy Øóx¶m1/M÷ÚÀ×§glpË^zªÕ]µó'ïÔ2a&gt;&gt;jµ`cá¡Ó¿W×ï?hElBo\Ðég }±x7ßÆü¸ñQÉü±RKòtIêôÈfMCvoè(&amp;RböædÆô4°wÍIÛ7Þ`Ø3ÌòÿpÄa@WÑNïH28Ã"FL¤óÜíT*ÒãåÞ'§3Ä{dè;}*0náM"OéÓ®³öL7Wå·YO°£ZH÷ûBi*ñaKûTÆ2n`eÖ§ÍÄñC¾2_rRõýX«3ñ²øR;=ôøRDcÜ~_b`ã6GçêT)Ô!Ù@¯Îhß4²Yÿ*uú¼Â
  ®Ñ!?wº¾Í?2ê?:Dªê%ú$ªænúÊg(|ÓVÃXè[$y¹Æ%¹õÓS;]¬ÙáËXð^ú09w)Ùá[4ucÙrðxñPKjG=aqøÍÀxl/worksheets/sheet1.xmlUÛnÛ0}ßWz_dé6IµYÖZô²&gt;+kEOµ_?J¾¦
  =E&lt;4©CYÿÉUT±õMFcNPH½[²ÇÍûÏ,²kÁjX²g°ì|õnq@³·`n³0`ÁTÀ&lt;¨íeÎó8¶I9·#,@'EsG¦ÙÅb¸L¹§ãñÇ8çR7só?¦25&amp;eÚÕ$wt'ÉÂ²Õ"d¸5«NI
  ·&amp;²esó|
  tuÖwr9Ä«EÜÅ Iä^¥È@ºd_&amp;óõÿ"|ðSÂÁÎãÛ{P8ÚËµEÜ{çwHfááâª!©)¡ïðpêd¡¶øh"\&gt;9§
  .A)_ünÊé«õ9ç¶°M.. å¥rç
  »ÎF35Ò\CêÎk2&gt;Æ¨*µ^sÇWÈÔòÜ÷|2QÅ}Äîõ¯VãE\QYIã»&lt;í¯#"½'Ç¤ëÓ¾#ÒÙÉJgÀééÀ³gÿ:5 ]ÓÈ`/YÊ¥¾ãöá¶³m=R?xßø7ª2×¯Qj`IMsùæÃ=ñáÕsÚ¾ï^ ×5zÄP?ÕkLö «µhúóÒáF*¦
  Y¡{à[¯ÙµzËGÏ°0R»"Ìy´ëg© î{¯t0Äwð]5Rg&lt;úÄ"SOB8;,Âz»Eç0o­,¥·&gt;xÑµFÃKÊ"*8Éw/_ I[),£%kFÏ7+ïÓ¥Ò=`7ý$Ë-T§Ý|i
  (oøZîÑ:6ÒX7xJéIºd»f¸;í±F§í(D½xèñÎt¬³×p{¦¥ »Ï?wC#õ¥u!GV0º ck(ÝÏê/PKjGÍKR"x#xl/worksheets/_rels/sheet1.xml.relsM1!{_A¶÷@cÌq×ù£Øp+a!,1ú{)-'yýä¤ÞÔ$¶p(bW¶ÈÞÂã~ÝAIGÞ0&amp;_XÝ|£}4b5&amp;,Bïõ¢µ¸@e*xgiûÀæuE÷BOúhÌI·ÿèåPKjGGD²Xð[Content_Types].xmlPKjG¶78éK_rels/.relsPKjG$±Vé¹xl/_rels/workbook.xml.relsPKjG3È8¼docProps/app.xmlPKjGòqå1µ¢sdocProps/core.xmlPKjGs{Y³¦Wxl/theme/theme1.xmlPKjG·ì£Ó;
  xl/sharedStrings.xmlPKjGâ_Þùh
  xl/styles.xmlPKjG´´!Ð4xl/workbook.xmlPKjG=aqøÍÀýxl/worksheets/sheet1.xmlPKjGÍKR"x#xl/worksheets/_rels/sheet1.xml.relsPKÑ¹
  </code></pre>
  
  <p>I am using this script;</p>
  
  <pre><code>       &lt;?php
  class Excel extends MY_Controller {
  
          public function __construct()
          {
              parent::__construct();
              include(APPPATH.'libraries/Classes/PHPExcel.php');
              include(APPPATH.'libraries/Classes/PHPExcel/IOFactory.php');
              $this-&gt;load-&gt;helper('download');
          }
  
          public function exporteerFactuur()
          {   
              // Create new PHPExcel object
              $objPHPExcel = new PHPExcel();
  
              // Set document properties
              $objPHPExcel-&gt;getProperties()-&gt;setCreator("Maarten Balliauw")
                               -&gt;setLastModifiedBy("Maarten Balliauw")
                               -&gt;setTitle("Office 2007 XLSX Test Document")
                               -&gt;setSubject("Office 2007 XLSX Test Document")
                               -&gt;setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
                               -&gt;setKeywords("office 2007 openxml php")
                               -&gt;setCategory("Test result file");
  
  
              // Add some data
              $objPHPExcel-&gt;setActiveSheetIndex(0)
                          -&gt;setCellValue('A1', 'Hello')
                          -&gt;setCellValue('B2', 'world!')
                          -&gt;setCellValue('C1', 'Hello')
                          -&gt;setCellValue('D2', 'world!');
  
              // Miscellaneous glyphs, UTF-8
              $objPHPExcel-&gt;setActiveSheetIndex(0)
                          -&gt;setCellValue('A4', 'aaa')
                          -&gt;setCellValue('A5', 'aaa');
  
              // Rename worksheet
              $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('Simple');
  
  
              // Set active sheet index to the first sheet, so Excel opens this as the first sheet
              $objPHPExcel-&gt;setActiveSheetIndex(0);
              // Redirect output to a client’s web browser (Excel2007)
              header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
              header('Content-Disposition: attachment;filename="01simple.xlsx"');
              header('Cache-Control: max-age=0');
              // If you're serving to IE 9, then the following may be needed
              header('Cache-Control: max-age=1');
  
              // If you're serving to IE over SSL, then the following may be needed
              header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
              header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
              header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
              header ('Pragma: public'); // HTTP/1.0
  
              $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
              $objWriter-&gt;save('php://output');
          }
  
          public function exporteer()
          {
  
          }
  }
  </code></pre>
  
  <p>The function exporteerFactuur() get called with an AJAX request.</p>