Error in exporting xls using phpexcel

Hi this is my code, actually i try to save the db values in xls format and also export(download) it, when i need. But i cant do the same functionality in single function. I can do it in either one process wether save/export, and i cant do it in diff function in same file also. Can any one know the reason?

Thanks in advance…


	$fileType = 'Excel5';
	$fileName = 'Sevens.xls';
	$phpExcel = new PHPExcel();

	$objWriter = PHPExcel_IOFactory::createWriter($phpExcel, $fileType);
	$objReader = PHPExcel_IOFactory::createReader($fileType);
	
	$objWriter->save($fileName);
	
	$objLoader = $objReader->load($fileName);					
	
		if($objReader->canRead($fileName)) {					

			$obj = $this->results("SELECT * FROM tbl_users"); // will return the no records in multi dim array.

			$i = 2;
			foreach ($obj as $datas => $rec) {
			$j = 65;

				foreach($rec as $coll => $value) {

					if(($i - 1) == 1) {
						$objLoader->getActiveSheet()->getColumnDimension(chr($j))->setAutoSize(true);
						$objLoader->getActiveSheet()->getStyle(chr($j).($i-1))->applyFromArray(array("font" => array( "bold" => true)));
						$objLoader->setActiveSheetIndex(0)													
								  ->setCellValue(chr($j).($i-1), $coll)
								  ->setCellValue(chr($j).($i+1), $value) ;
					  }

					$objLoader->setActiveSheetIndex(0)
							  ->setCellValue(chr($j).$i, $value);
					$j += 1;
					}
				$i += 1;			
			}		

// header(“Content-Type: application/vnd.ms-excel”);
// header(“Content-Disposition: attachment; filename=\“filename.xls\””);
// header(“Cache-Control: max-age=0”);

			$objWriter = PHPExcel_IOFactory::createWriter($objLoader, $fileType);

//I can do either one of the follwoing line, i cant do both in same function in same file*

// $objWriter->save($fileName);
// $objWriter->save(‘php://output’);

		}