PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Phpexcel create wrong file

 January 23, 2022     codeigniter, codeigniter-3, excel, php, phpexcel     No comments   

Issue

this is my simple code to create an xlsx file and open it with openoffice

require(APPPATH.'/PHPExcel-1.8/Classes/PHPExcel.php');
require(APPPATH.'/PHPExcel-1.8/Classes/PHPExcel/Writer/Excel2007.php');
$this->load->library('excel');
$objPHPExcel = new PHPExcel();

$objPHPExcel->setActiveSheetIndex(0)
   ->SetCellValue('A1', 'Calendario Attivit');

$filename = "prova".".xlsx";
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Disposition: attachment;filename="'.$filename.'"');
header("Cache-Control: max-age=0");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  
$objWriter->save('php://output');
exit;

the file is downloaded but when open it i get this:

enter image description here

someone can help me ?


Solution

Try to add ob_end_clean() before the output :

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  
ob_end_clean();
$objWriter->save('php://output');


Answered By - Hasta Dhana
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing