getting corrupted xlsx files when saving to browser.

Topics: Developer Forum, User Forum
Jun 26, 2012 at 12:35 PM
Edited Jun 26, 2012 at 12:46 PM

I want to generate a report (.xlsx) from database 

I am getting corrupted xlsx files when saving/pushing to browser. i.e. using


But if I save the file locally on the disk  i.e. using

$objWriter->save("Bidding Report $excel_title.xlsx");

 then it results perfect .xlsx

I have checked there is no whitespace

even it was working good , many days ago. but now it stuck. here is my code.

Please look into the code and tell me where i am wrong. it really very complex to debug

require_once 'excel/PHPExcel.php';
require_once 'excel/PHPExcel/Writer/Excel2007.php';
global $db;
$from = $_GET['f'];
$to = $_GET['t'];
$excel_title = date('d.M.y',$from) . "--" . date('d.M.y',$to);
//$input = array("red", "green", "blue", "yellow");
//var_dump(array_splice($input, -2));
$arr_bid = $db->get_results($stmt,ARRAY_A);

if( !empty($arr_bid) ) {
    foreach ($arr_bid as & $bid) {
       //loop though each sub array and slice off the first 6 to a new multidimensional array
//@note : unset() is must if using  rray_splice() to remove  `&` in foreach
//@note : use $arr_report array  if we use array_slice()


$cellHeader = array('Auction Number',
                'Bidder Name', 'Address', 'City', 'State', 'Zip Code', 'E-mail',
                'Donator Name', 'Address', 'City', 'State', 'Zip Code', 'E-mail', 
                'Charity Name', 'Address', 'City', 'State', 'Zip Code','E.I.N');

            // Create new PHPExcel object
            //echo date('H:i:s') . " Create new PHPExcel object\n";
            $objPHPExcel = new PHPExcel();
            // Set properties
            //echo date('H:i:s') . " Set properties\n";
            $objPHPExcel->getProperties()->setCreator("Brian Schneider");
            $objPHPExcel->getProperties()->setLastModifiedBy("Brian Schneider");
            $objPHPExcel->getProperties()->setTitle("Charity Rummage Bidding Report");
            $objPHPExcel->getProperties()->setSubject("Charity Rummage Bidding Report");
            $objPHPExcel->getProperties()->setDescription("History of all bids placed on charityrummage generated By Admmin");
            // Add some data
            $i = 0;
            foreach ($cellHeader as $v) {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($i, 1, $v);
                //set width of each column
                $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($i)->setWidth(strlen($v) + 5);
                //set heading row to bold 
            if( !empty($arr_bid) )
                foreach ($arr_bid as $row)
                    foreach($row as $col)
                       // echo "<br/>Column:".$c;
                       // echo "&nbsp;&nbsp;Row:".$j;
                       // var_dump($col);
                          $objPHPExcel->getActiveSheet()->getColumnDimensionByColumn($c)->setWidth(strlen($col) + 6);
                          $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($c, $j, $col);
            else {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 2, 'No Bidding History');
            // Rename sheet
            $objPHPExcel->getActiveSheet()->setTitle('Bidding Report');
            // Set active sheet index to the first sheet, so Excel opens this as the first sheet
            // Redirect output to a client�s web browser (Excel2007)
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
            //$objWriter->save("Bidding Report $excel_title.xlsx");
            // ask user to save on his system
            // This is MUST to write, otherwise corrupt xlsx will be generated
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
            header("Cache-Control: no-store, no-cache, must-revalidate");
            header("Cache-Control: post-check=0, pre-check=0", false);
            header('Cache-Control: max-age=0');
            header("Pragma: no-cache");
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename=BiddingReport'.$excel_title.'.xlsx');
            // Save Excel 2007 file           
            //echo date('H:i:s') . " Report sheet generated.\r\n";
below is final `$arr_bid` structure :
$arr_bid =array
  0 => 
      'auctionNumber' => string 'AU790854' (length=8)
      'bidderName' => string 'Adman Smith' (length=11)
      'bidderAddress' => string 'test' (length=4)
      'bidderCity' => string 'jaipur' (length=6)
      'bidderState' => string 'rajasthan' (length=9)
      'bidderZip' => string '302014' (length=6)
      'bidderEmail' => string '' (length=18)
      'donorName' => string 'Steve Jobs' (length=13)
      'donorAddress' => string 'test' (length=4)
      'donorCity' => string 'jaipur' (length=6)
      'donorState' => string 'rajasthan' (length=9)
      'donorZip' => string '302014' (length=6)
      'donorEmail' => string '' (length=28)
      'churchName' => string 'ACE COMMUNITY PARK INC' (length=22)
      'churchAddress' => string 'PO BOX 321' (length=10)
      'churchCity' => string 'ACE' (length=3)
      'churchState' => string 'TX' (length=2)
      'churchZip' => string '77326-0321' (length=10)
      'churchCode' => string '760133257' (length=9)
  1 => 
      'auctionNumber' => string 'AU769810' (length=8)
      'bidderName' => string 'testuser testing' (length=16)
      'bidderAddress' => string 'zckknk' (length=6)
      'bidderCity' => string 'kknkj' (length=5)
      'bidderState' => string 'knknk' (length=5)
      'bidderZip' => string 'knkkn' (length=5)
      'bidderEmail' => string '' (length=15)
      'donorName' => string 'Keshav Mohta' (length=12)
      'donorAddress' => string '101/91,Pratap nagar' (length=19)
      'donorCity' => string 'Jaipur' (length=6)
      'donorState' => string 'Rajasthan' (length=9)
      'donorZip' => string '302022' (length=6)
      'donorEmail' => string '' (length=21)
      'churchName' => string 'ACE COMMUNITY PARK INC' (length=22)
      'churchAddress' => string 'PO BOX 321' (length=10)
      'churchCity' => string 'ACE' (length=3)
      'churchState' => string 'TX' (length=2)
      'churchZip' => string '77326-0321' (length=10)
      'churchCode' => string '760133257' (length=9)
Jun 26, 2012 at 1:21 PM

The standard answer to this question:

Open the file in a text editor, and look for any whitespace characters at the beginning or end of the file, or any obvious PHP error messages in the file... in which case your script is echoing something.

Jun 26, 2012 at 1:24 PM

I solved the issue.

Actually I called above file(`generate.php`) from another page where action is given like this

action ="page.php?page=generate&f=123&t=456"

and  in page.php , file we manipulate with query string , also there is many JS and CSS files included. that why i was getting corrupt file


now resolved just by changing

action = "generate.php?&f=123&t=456"


Sorry to bother you.