Saturday, June 9, 2012

Back up into ms excel php

Back up into ms excel php


Some users need to have the mysql data in to ms excel format to manipulate data easily with their knowledge on excel.In here too,I have used same table as in previous tutorial("dept_division").
Run and see the bellow scripts.


<?php

$database="my_database";
$table="dept_division";
$uer="root";
mysql_connect("localhost",$uer,"");
mysql_select_db("my_database");

$result=mysql_query("select * from $table");

$out = '';

$fields = mysql_list_fields(my_database,$table);


$columns = mysql_num_fields($fields);


for ($i = 0; $i < $columns; $i++) {
$l=mysql_field_name($fields, $i);
$out .= '"'.$l.'",';
}
$out .="\n";


while ($l = mysql_fetch_array($result)) {
for ($i = 0; $i < $columns; $i++) {
$out .='"'.$l["$i"].'",';
}
$out .="\n";
}


$f = fopen ('export.csv','w');


fputs($f, $out);
fclose($f);

header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="export.csv"');
readfile('export.csv');
?>
Next

No comments:

Post a Comment