Oct
10

How to convert SQL to Excel in Php?

Author Arpi    Category PHP, Tips     Tags

Many a times while developing a project we want to send the query data to the user or we want to have the query data for any other reason like analysis or report etc.

So to convert the SQL query data into the excel sheet form is many a times useful for developers.

You can do it using following code,

<?      $sql = "select * from wp_db";
	$result = mysql_query($sql);
	$arr_data = array();

	while($data = mysql_fetch_assoc($result)){
                   $arr_data[] = $data;
		}

	$sep = "\t"; //tabbed character
	$fp = fopen('report.xls', "w");
	$schema_insert = "";
	$schema_insert_rows = ""; 

	$schema_insert_rows.= "\t Query Result in Excel \n"; 

	$schema_insert_rows.="Insert Headings here for each fields\t";
	$schema_insert_rows.="\n";
	fwrite($fp, $schema_insert_rows);

	foreach($arr_data as $k=>$v){
	$schema_insert .= implode("\t",$v)."\n";
	}

	fwrite($fp, $schema_insert);
	fclose($fp);
?>
save your file and run the code.
You will find one excel sheet saved in the same folder
as your file containing this code is residing.
 

Arpi

hiii friends… i am doing msc in computer application and information technology. we are here to share our knowledge and idea on shareprogrammingtips.com.

More PostsWebsite

You may like to read this also....

1 Comment to “How to convert SQL to Excel in Php?”

  • anonym September 21, 2012 at 11:37 am

    Thnx :-)

Post comment

*

CommentLuv badge
Follow us on Twitter! Follow us on Twitter!

Search in this website

our sponsors

latest comments

Find us on Facebook

Top Authors

35 posts
saurabh
22 posts
10 posts

Find us on stackoverflow

Polls

Tell us who you are

View Results

Loading ... Loading ...

My Bookmarks

Sponsers Link