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;
PKjGGD²Xð[Content_Types].xmlMNÃ0÷"ò%nY vAa (0ö¤±êØgúw{&i#ÕnbEö{ßøyìÑdÛ¸l
mð¥×ÁX¿(ÅÛü)¿òF¹à¡;#1_æ»±Øc)j¢x/%êEày¦
©QÄ¿i!£ÒKµy3ÜJ<§Z1½0?YÙL%zV
cäÖIb7Øûìa/lÙ¥P1:«qáríÍjªÊj0A¯Íuë""íàÙ( ÁWìMä)Tjå({ܲû>óOãÂ,XÙÁÚÆ>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)åØõ>ªqQCD¦ãb!]þi$Lò3´èÉÔ2nÊòÃoT¦:Z
áh· ê³ç[ØÒ4½á·]ºÐyNì,Û¹>¤>£j
-'
VÌ)#÷EF^6ÚÜnô÷´8q"KÐHàë>_×Öÿ¹¢eÆÍ<âáUdøvÁÅ
TPKjG$±Vé¹xl/_rels/workbook.xml.relsÁjÃ0Dïý
±÷ZvZJ)s)\[÷´¶LlIh7mý÷UH¡Ä¬ØÇHëÍ÷8OLÔ¯ *JèM°½ï|4ÛûgÄÚ[=
&$ØÔwë74çr}$M<)pÌñEJ2GMEèóMÒ¨9ËÔɨÍ^w(Weù$ÓÜêO±³
ÒÎV )â¼CÛö_9èùJ$Ì/:dG]d×ãWKÆsÞÅsú<«[VàtBûÎ)?ð¼ùøÌã0_!íÉ!òä4úEÍÇ©yñãêPKjG3È8docProps/app.xmlSÁnÛ0½ï+Ý9Á0¢¢H7ô°bvgV¦c¡²$¬ìë';°ë¬;M§ÇÇçgR·§Ö&²ÁoÄrQ½ õÇx:|¿ù*
bð¸àq#ÎHâVR»"&¶HEvð´
s\KI¦ÁhÛ>wêZà\¦£um
ÞóÖ¢g¹*Ë/O¾Âê&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>0-2Ö¸¯«w%·7¶ÎXpAGQ¤}ÁíC°!¡a~ ãâÒÕÄ2þÈj Ë<¿"
&X`dfv6âI)ø¬´SI 8
èà ]PòÌp¿¸!UÎÈFÞÂEôTé'/g°ëºE·Jhìýæþ!I=DÅW¥àwÀqÕ1#£¦dmW³â¤b>lbä ⦿À¿fÊiÐÑÅqSåçêëíîWË~Ê(Íh¾£WÅòc±¢ïsZäùÐÇɳµNú?íÉÒ2(¨¾¹}Fûû=ÚèÖðv¸ËÌ[xî¤
ñiVâ[Bÿª> 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¶>¸7UDb<¸kEJ¥¶-}Æò"OIk.b¬`(B;øØÆÌn6m;Æ4±Pcàzs2¡>A£¥µµ`>`ð(MøLø¹D"ÇS'ûsÙgbÖµ#NÀFä²ÃRÁB×jäËÞºl/ZC«Ñ
óOIWÓfN'ÂñкvüÿUÜ`0è%¿},uV°î°ãô<5Pñ¸Ê»ßðn¯ñoà7z½·Q÷*¼»ï4Úîv³w+¼·ªo»ßo×ð^o¯à6Ún"Fé
:ç22Kȳ«FxàŨP¶¶»
úDÛk1¾ËÅyp±¢ RóL°¸>ÇâLÞ$X[)¦|¹2ÉBÒ4U]ëãCFTO|ñô1zñôÑñý'Ç÷9~ðàøþÏ«8 uÂçßñ÷·¢¿÷üáWf¼Ôñ¿ÿôÙo¿~i*øìëG<yôìÏÿü᡾-ðXhL$ºAÐ>Á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Ùç§<âj¸Pàü ®>¡*:p
b\B(KÖ¡D)p»°Öòί¨lÎç¼Å½ÐXíò né÷Í%|J]P+cpZaKo&Ì)§æxfiÞK¥Ù7!oÎ^&8íf!6
f$Èü^0XåÜC$#2FѧuJ·u^í5MÚFëͤ&Hº8w8ï¢ÔX½,©Ðhå5=ù8íZè¹à1NÌJfaÒµ|UòÊd>i°y[:µ×D¤Bª,£*_Z¼I*ýùá|°_WVÇùµ°OL&ÄWkfªa¹Æg(8Bc6ûôvÝP GEs1¡n¹ñê_fÁÉ×>ev`F¸¬I-ö<^ê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´¨&¥#rÜÉ Ù¦ìÃÿpçã6^§=¨¹géE\èkGÁÆ©pÆ£¶i¶¸éú¨Má²/(ÜTø¬êoG|¢%x¡S¦ßrr:w4ã2Vo·ªBÐi¼ýæSsvk³·ãlÏàkï家WSÔÖ.2ùhåÏ,>¾²wà~4cJïîÁ¥´¿øøØéÖ?PKjG·ì£Óxl/sharedStrings.xml]ÎAj1Ð}Oájßñ$Pí,¡h buÆ`ËK¦·¯K,ÿû|øîp+Ù\©Iªìa3`Ï5&<|~^ÞÀ"GÌÉà ÂQÓ§,fÕeog*(C]{óU[Aí±MVFe&Òívw¶`b0+§ËJǺ²zxà$§ár®Îjpöþñ»¶ïdû«ðPKjGâ_Þùh
xl/styles.xmlËn0÷}
ËûÃRµ5ºhT5TêÖÀV|Aöp
yúÚÃE6Ìü>?û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î]æõ×DZÏ*-´!¦-Sç§ùò²brA<2ÁKýçump!Ö¾Î4YÒ3D0*w Yâbêak
uT·MñùÖcÛÇ}áò ò¸°uí¨ë~ó͵]jS»o¿74HþÁeÑùB<ûïý·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>>jµ`cá¡Ó¿W×ï?hElBo\Ðég }±x7ßÆü¸ñQÉü±RKòtIêôÈfMCvoè(&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ô²>+kEOµ_?J¾¦
=E<4©CYÿÉUT±õMFcNPH½[²ÇÍûÏ,²kÁjX²g°ì|õnq#³·`n³0`ÁTÀ<¨íeÎó8¶I9·#,#'EsG¦ÙÅb¸L¹§ãñÇ8çR7só?¦25&eÚÕ$wt'ɲÕ"d¸5«NI
·&²esó|
tuÖwr9Ä«EÜÅ Iä^¥È#ºd_&óõÿ"|ðSÂÁÎãÛ{P8Ú˵EÜ{çwHfááâª!©)¡ïðpêd¡¶øh"\>9§
.A)_ünÊé«õ9綰M.. å¥rç
»ÎF35Ò\CêÎk2>ƨ*µ^sÇWÈÔòÜ÷|2QÅ}Äîõ¯VãE\QYIã»<í¯#"½'ǤëÓ¾#ÒÙÉ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<úÄ"SOB8;,Âz»Eç0o,¥·>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&_XÝ|£}4b5&,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ѹ
I am using this script;
<?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->load->helper('download');
}
public function exporteerFactuur()
{
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'aaa')
->setCellValue('A5', 'aaa');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->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->save('php://output');
}
public function exporteer()
{
}
}
The function exporteerFactuur() get called with an AJAX request.
I would try the CodeIgniter Download Helper if the file is not too big, and you don't have memory constraints.
// The Download helper handles most of the HTTP headers but you could still send some if you like
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('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header('Pragma: public'); // HTTP/1.0
// Load the download helper
$this->load->helper('download');
// Save and capture output (into PHP memory)
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
ob_start();
$objWriter->save('php://output');
$excelFileContents = ob_get_clean();
// Download file contents using CodeIgniter
force_download('01simple.xlsx', $excelFileContents);
If you'd prefer to do it all yourself, I would also add header("Content-Transfer-Encoding: binary"); and check the code for CodeIgniter's force_download to get a clue of which headers are sent by the helper.
Related
I'm using the basic phpexcel 01simple-download-xlsx.php as Is
Didnt add any code of my own for now ..
And I get unexpected results
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'hello');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
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, 'Excel5');
$objWriter->save('php://output');
exit;
And this is what i get
https://i.imgur.com/GKc5xRq.png
The problem was that some other characters were printed before my function
I was trying to export data to excel file with a QRcode within a cell.
I googled up and tried for clue.
Finally thought with adding a sample image file. then after with a phpqrcode image file.
It is still not succeeded can you please help me out.
Code:
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$gdImage = imagecreatefromjpeg('abstract_bg.jpg');
//Add a drawing to the worksheet
echo date('H:i:s') . " Add a drawing to the worksheet\n";
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
// Set document properties
$objPHPExcel->getProperties()->setCreator("Jimson Jose")
->setLastModifiedBy("Jimson Jose")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("For minimizing work force by jimson, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Rewards");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->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->save('php://output');
exit;
?>
When generating a file, you should make no debug output like
echo date('H:i:s') . " Add a drawing to the worksheet\n";
because it corrupts binary output.
If you remove this line, your code will work.
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$gdImage = imagecreatefromjpeg('abstract_bg.jpg');
//Add a drawing to the worksheet
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
//$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
// Set document properties
$objPHPExcel->getProperties()->setCreator("Jimson Jose")
->setLastModifiedBy("Jimson Jose")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("For minimizing work force by jimson, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Rewards");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->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->save('php://output');
exit;
?>
I would also recommend removing error reporting from this script:
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
have a problem to download excel file. When im saving on server its worked ok. But when i try to download things goes wrong
enter code hererequire_once('Classes/PHPExcel.php');
$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp;
$cacheSettings = array( ' memoryCacheSize ' => '1024MB');
ini_set("memory_limit","-1");
ini_set('max_execution_time', 13600);
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Techizer")
->setLastModifiedBy("Techizer")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setCategory("Test result file");
if($file=="employer")
{
// Rename worksheet
$file = 'employer';
$objPHPExcel->getActiveSheet()->setTitle($file);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1','Employer Name')
->setCellValue('B1','Emp ID')
->setCellValue('C1','Designation')
->setCellValue('D1','Zone')
->setCellValue('E1','Password');
$query = $this->session->userdata('employ');
$query = preg_replace('/LIMIT.*/', '', $query);
$res = $this->common_model->execute_query($query);
$row = 2;
foreach($res as $result)
{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$row,$result->profile_name)
->setCellValue('B'.$row,$result->profile_empid)
->setCellValue('C'.$row,$result->profile_desig)
->setCellValue('D'.$row,$result->profile_zone)
->setCellValue('E'.$row,$result->profile_pass);
$row++;
}
header('Content-Disposition: attachment;filename=Employer list'.date("Y-m-d").'.xls');
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
//header('Content-Disposition: attachment;filename=report.xls');
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, 'Excel5');
$objWriter->save('php://output');
exit;
it gives an excel file with this content:
PK–jGG’D²Xð[Content_Types].xml”MNÃ0…÷œ"ò%nY
I'm trying to download my excel file with PHPExcel and it will download a file..but it's empty..! Basically I tried to modify the 01simple.php example (which works fine when the file is saved into the server) to download the file. But it is an empty file. I read few examples and topics around but still I'm a bit confused. this is my code. thank you
<?php
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
/*
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');*/
/** Include PHPExcel */
require_once dirname(__FILE__) . '/PHPExcel/Classes/PHPExcel.php';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("at")
->setLastModifiedBy("At")
->setTitle("title")
->setSubject("Libro ")
->setDescription("Libro")
->setKeywords("excel openxml php")
->setCategory("At");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C1', 'Hello')
->setCellValue('D2', 'world!');
// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A4', 'Miscellaneous glyphs')
->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Libro Soci aa. 2015');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->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="Libro Soci 2015.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->save('php://output');
exit;
solved by adding ob_end_clean at the end of the sheet just output
As the simple php excel we get the function setCellvalue('A1','product').But i wanted to make this column to change dynamically by placing it in loop.MY code is given below
<?php
/**
* Created by PhpStorm.
* User: Anurag
* Date: 3/9/14
* Time: 6:09 PM
*/
$Selected_data=$_REQUEST['data'];
/*
echo'<pre>';
print_r($Selected_data);
echo'<pre>';
*/
$Parse_Selected_data=json_decode($Selected_data,TRUE);
/*
echo'<pre>';
print_r($Parse_Selected_data);
echo'<pre>';
*/
$row_count=$Parse_Selected_data[0]['row_count'];
//echo($row_count);
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
/** Include PHPExcel */
require('Excel/PHPExcel.php');
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
/*
* Add the Items to the Excel sheet*/
//**************** SET THE HEADER ***********//
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue(chr(65),'S.NO');
//******************************* SET AUTO COLUMN WIDTH **************//
$objPHPExcel->getActiveSheet(0)->getColumnDimension('A')->setAutoSize(true);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('C')->setAutoSize(true);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('D')->setAutoSize(true);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('E')->setAutoSize(true);
$objPHPExcel->getActiveSheet(0)->getColumnDimension('F')->setAutoSize(true);
//**************** SET HE SIZE AND BOLD ****************************//
$objPHPExcel->getActiveSheet(0)->getStyle('A1')->getFont()->setBold(true)->setSize(14);
$objPHPExcel->getActiveSheet(0)->getStyle('B1')->getFont()->setBold(true)->setSize(14);
$objPHPExcel->getActiveSheet(0)->getStyle('C1')->getFont()->setBold(true)->setSize(14);
$objPHPExcel->getActiveSheet(0)->getStyle('D1')->getFont()->setBold(true)->setSize(14);
$objPHPExcel->getActiveSheet(0)->getStyle('E1')->getFont()->setBold(true)->setSize(14);
$objPHPExcel->getActiveSheet(0)->getStyle('F1')->getFont()->setBold(true)->setSize(14);
//***************** Add some data **************//
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A2', 'Hello')
->setCellValue('B2', 'world!')
->setCellValue('C2', 'Hello')
->setCellValue('D2', 'world!');
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
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, 'Excel5');
$objWriter->save('php://output');
exit;
?>
Please let me know how to get it done.
Try this..
//$i = asci value i.e. 65 to any column asci
$cell=chr($i).$num; // u can dynamically change the number also.
$objWorksheet->setCellValue($cell , 'Comments:');
If you have more than A-Z columns you can use this function:
function getCellFromColnum($colNum) {
return ($colNum < 26 ? chr(65+$colNum) : chr(65+floor($colNum/26)-1) . chr(65+ ($colNum % 26)));
}