Kamis, 07 Oktober 2010

Export Mysql to excel

mengekspor data dari database mysql ke  microsoft excel menggunakan script php, silahkn copy pastekan kode di bawah ini kemudian jalankan dengan pogram php anda
<?php
mysql_connect("localhost","root","root");
mysql_select_db("Karyawan");
$qry_data=mysql_query("SELECT * FROM pribadi ORDER BY nip");


function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
    return;
}
function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}
function xlsBuatBaris($data, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $data, $Col, 0x0);
    echo pack("d", $Value);
    return;
}
function xlsBuatLabel($data, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $data, $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=datapribadi.xls ");
header("Content-Transfer-Encoding: binary ");


xlsBOF();

xlsBuatLabel(0,0,"DAFTAR DATA PRIBADI");

xlsBuatLabel(2,0,"NIP");
xlsBuatLabel(2,1,"NAMA");
xlsBuatLabel(2,2,"ALAMAT");
xlsBuatLabel(2,3,"KELAMIN");
xlsBuatLabel(2,4,"TGL LAHIR");

$xlsRow = 3;

while($data=mysql_fetch_array($qry_data)){
    xlsBuatBaris($xlsRow,0,$data['nip']);
    xlsBuatLabel($xlsRow,1,$data['nama']);
    xlsBuatLabel($xlsRow,2,$data['alamat']);
    xlsBuatLabel($xlsRow,3,$data['kelamin']);
    xlsBuatLabel($xlsRow,4,$data['tgl_lahir']);
   
    $xlsRow++;
}   

xlsEOF();
exit();
?>

1 komentar:

  1. coba pake PEAR bro

    http://ikhsanaulia.blogspot.com/2010/11/mengekspor-data-ke-file-excel-dengan.html

    BalasHapus