Rifky Apriadi

July 11, 2008

expot mysql to excel

Filed under: PHP

kemaren lg nyari cara import dari excel ke mysql.. cos banyak data maual dari excel mau d conversi k mysql nh! tapi dapetnya malah dari mysql k excel.. nih sourcenya, sapa tau berguna :

<?

// Connect database.
mysql_connect("localhost","root","");
mysql_select_db("dbkar");

// Get data records from table.
$result=mysql_query("select * from tbkar");

// Functions for export to excel.
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=orderlist.xls ");
header("Content-Transfer-Encoding: binary ");

xlsBOF();

/*
Make a top line on your excel sheet at line 1 (starting at 0).
The first number is the row number and the second number is the column, both are start at ‘0′
*/

xlsWriteLabel(0,0,"Karyawan");

// Make column labels. (at line 3)
xlsWriteLabel(2,0,"NIK");
xlsWriteLabel(2,1,"Name");
xlsWriteLabel(2,2,"Telephone");
xlsWriteLabel(2,3,"Address");
$xlsRow = 3;

// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){

xlsWriteNumber($xlsRow,0,$row[’nik_kar’]);
xlsWriteLabel($xlsRow,1,$row[’name_kar’]);
xlsWriteLabel($xlsRow,2,$row[’tlp_kar’]);
xlsWriteLabel($xlsRow,3,$row[’adrs_kar’]);

$xlsRow++;
}
xlsEOF();
exit();
?>

Comments »

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Ian Main