Export MySQL data to Excel in PHP


How to Export XLS file in PHP:

SOlution:



		$date_format = 'n/j/Y g:i:s A';
		while(@ob_end_clean());
		require_once(dirname(__FILE__).'/RecipeExcelWriter.class.php');
		$xls = new RecipeExcelWriter();
		$xls->addRow(array(
		  'Recipe ID',
		  'Recipe Name',
		  'Chef Name',
		  'Dishtype',
		  'Status',
		  'Date Added',
		  'Date Updated ',
		  'Utensils',
		  'Ingredients',
		  'Video',
		));
		
		
		foreach($items as $item){
		
			if($feu->MemberOfGroup($item['user_id'],$feu->GetGroupID($this->GetPreference('feu_group'))))
			$status = 'Chef';
		  elseif($feu->MemberOfGroup($item['user_id'],$feu->GetGroupID('New Users')))
			$status = 'New User';
		  else
			$status = '(none)';
			
			if($item.recipe_status == 0){
				$Recipestatus = 'In Progress';
			}else if($item.recipe_status == 1){
				$Recipestatus = 'Submitted';
			}else if($item.recipe_status == 2){
				$Recipestatus = 'Approved';
			}else if($item.recipe_status == 3){
				$Recipestatus = 'Published';
			}
			
			$ingredient = $this->getIngredients($item['recipe_id']);
			
			
  
  			$utensils = $this->getUtensils($item['recipe_id']);
			$ing_title = '';
			$utensil_title = '';
				foreach($ingredient as $ing){
					$ing_title .= $ing['title'];
				}
		
				foreach($utensils as $uten){
					$utensil_title .= $uten;
				}
			
			
			if($this->getVideo($item['recipe_id']))
			$Indicator = 'Yes';
			else
			$Indicator = 'No';
			
			$xls->addRow(array(
			$item['recipe_id'],$item['recipe_title'],
			
			$feu->GetUserPropertyFull('first_name',$item['user_id'],$item['user_id']).' '.$feu->GetUserPropertyFull('last_name',$item['user_id'],$item['user_id']),

			str_replace('\'','\\\'',$this->getMultipleCategory($this->getRecipeCategory($item['recipe_id'],'dishtype'),true)),
			
			/*str_replace('\'','\\\'',$this->getCategory($this->getRecipeCategory($item['recipe_id'],'mainingredient'),true)),
			
			str_replace('\'','\\\'',$this->getCategory($this->getRecipeCategory($item['recipe_id'],'dishtype'),true)),*/
			$Recipestatus,
			
			date($date_format,strtotime($item['recipe_added'])),
			
			date($date_format,strtotime($item['recipe_updated'])),
						
			$utensil_title,
			
			$ing_title,
			
			$Indicator,  
		     ));
		}
		
		$xls->send('chefs.'.date('Y-m-d.Hi'));
		exit;



<strong>Class file for export XLS file (RecipeExcelWriter.class.php'):</strong>

data .= "  <tr>\n";
      foreach($line_arr as $value)
        $this-&gt;data .= "    <td class="xl24" width="64">{$value}</td>\n";
      $this-&gt;data .= "  </tr>\n";
    }

    function send($filename = null){
	//print_r($_REQUEST);exit;
      if($filename){
        header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" );
        header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" );
        header ( "Pragma: no-cache" );
		//if($_REQUEST['m1_recipe_xls'] == 1){
	    header ( "Content-type: application/x-msexcel" );
        header ( "Content-Disposition: attachment; filename={$filename}.xls" );
		/*}else{
		header('Content-type: application/octet-stream');
		header('Content-Disposition: attachment; filename="filename.csv"');
		}*/

      }
     
    }