php export to excel - Web Development and Web Design Codes

Latest

Friday, April 20, 2018

php export to excel

php export to excel -export php array to excel


php export to excel -export php array to excel
php export to excel -export php array to excel
Hi Friends i hope you are fine..in this lesson we will learn how to convert php array to excel and export as excel..this is beginners codes how oto export php file to excel..please use the below codes to php export to excel..

1. exportexcel.php
<?php
    $data = array(
	           array('Name'=>'CodeNair',
			         'Age'=>'6 Months',
			         'Category'=>'Web Design and Web Development'
			         ),
			   array('Name'=>'CodeNair',
			         'Age'=>'6 Months',
			         'Category'=>'Web Design and Web Development'
			         ),
					array('Name'=>'CodeNair',
			         'Age'=>'6 Months',
			         'Category'=>'Web Design and Web Development'
			         )
					 
	    );
    //Filter Function to FIlter Data
    function filterData(&$str)
    {
        $str = preg_replace("/\t/", "\\t", $str);
        $str = preg_replace("/\r?\n/", "\\n", $str);
        if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
    }
    
    //File Name
    $fileName = "array_to_exel_codenair" . date('Ymd') . ".xls";
    
    // headers for force download
    header("Content-Disposition: attachment; filename=\"$fileName\"");
    header("Content-Type: application/vnd.ms-excel");
    
    $flag = false;
    foreach($data as $row) {
        if(!$flag) {
            //Header Column
            echo implode("\t", array_keys($row)) . "\n";
            $flag = true;
        }
        //filtering data
        array_walk($row, 'filterData');
        echo implode("\t", array_values($row)) . "\n";

    }
    
    exit;
?>

That's it Friends how to export php data as excel ..if you like this post please share with your friends..Thank You..

No comments:

Post a Comment

Thank You for Your Comment

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