membuat report excel dari php

nah, sekarang aku mau buat report dengan php nih yang outputnya pake excel, ini contohnya untuk buat laporan harian :

<?php
// Connect database.
mysql_connect("localhost","root","");
mysql_select_db("dbku");
$strnama = ($_POST['nm_kar']);
$strweek = ($_POST['week']);
// Get data records from table.
$result=mysql_query("select * from dailyreport where nm_kar='".$strnama."' and week_rep='".$strweek."'");
// 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=Daily.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,"PT.IIP,Tbk");
xlsWriteLabel(2,0,"Nama : ".$strnama."");
xlsWriteLabel(3,0,"Daily Report");
// Make column labels. (at line 3)
xlsWriteLabel(5,0,"Name");
xlsWriteLabel(5,1,"Week");
xlsWriteLabel(5,2,"Date");
xlsWriteLabel(5,3,"Activity");
xlsWriteLabel(5,4,"Status");
$xlsRow = 6;
// Put data records from mysql by while loop.
while($row=mysql_fetch_array($result)){
xlsWriteLabel($xlsRow,0,$row['nm_kar']);
xlsWriteLabel($xlsRow,1,$row['week_lap']);
xlsWriteLabel($xlsRow,2,$row['date_lap']);
xlsWriteLabel($xlsRow,3,$row['activity_lap']);
xlsWriteLabel($xlsRow,4,$row['stat_lap']);
$xlsRow++;
}
xlsEOF();
exit();
?>

ya, mungkin itu berguna. (cuma share aja) :D

Leave a Reply