Thursday, July 12, 2007

Easy way to create XLS Excel file from PHP

Everybody knows phpMyAdmin can export file to Excel format but phpMyAdmin just export .csv file,not real Excel file format. If you are interest in PHP programming and need to export to the real Excel format please check it out !

Example PHP export to XLS file format.

1. Create Function for XLS

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;
}



2. Send Header to Browser in download format.

// Query Database
$result=mysql_db_query($dbname,"select id,prename,name,sname,grade from appdata where course='$courseid' and sec='$section'")

// Send Header
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=$courseid-$sec.xls ");
header("Content-Transfer-Encoding: binary ");

// XLS Data Cell

xlsBOF();
xlsWriteLabel(1,0,"Student Register $semester/$year");
xlsWriteLabel(2,0,"COURSENO : ");
xlsWriteLabel(2,1,"$courseid");
xlsWriteLabel(3,0,"TITLE : ");
xlsWriteLabel(3,1,"$title");
xlsWriteLabel(4,0,"SETION : ");
xlsWriteLabel(4,1,"$sec");
xlsWriteLabel(6,0,"NO");
xlsWriteLabel(6,1,"ID");
xlsWriteLabel(6,2,"Gender");
xlsWriteLabel(6,3,"Name");
xlsWriteLabel(6,4,"Lastname");
$xlsRow = 7;
while(list(
$id,$prename,$name,$sname,$grade)=mysql_fetch_row($result)) {
++
$i;
xlsWriteNumber($xlsRow,0,"$i");
xlsWriteNumber($xlsRow,1,"$id");
xlsWriteLabel($xlsRow,2,"$prename");
xlsWriteLabel($xlsRow,3,"$name");
xlsWriteLabel($xlsRow,4,"$sname");
$xlsRow++;
}
xlsEOF();
exit();

source from www.appservnetwork.com

9 comments:

piya said...

ไม่ทราบว่าจะตั้งชื่อ Function ในการ สร้างไฟล์ XLS ว่าอย่างไร ได้โปรด

superkeng said...

ถ้าจะสร้างเป็นฟังก์ชั่น ก็ตั้งชื่อฟังก์ชั่นว่าอะไรก็ได้ครับ เพียงแค่ส่งค่าเข้ามามันก็จะใช้ได้แล้ว

แต่ในตัวอย่างเป็นการเรียกฟังก์ชั่น excel ด้านบนมาใช้เลยนะครับ แค่ก้อปมาไว้ให้อยู่ในหน้าเดียวกันเท่านั้นเอง

marker said...

You might want to try this article from zedwood.com which has some sample code on how to generate a multi-worksheeted xls spreadsheet with formatting. It uses the PHP Pear package, but you can just download the code and run it. Its great.

Your code generates spreadsheets in excel 2.0 format. The zedwood code generates xls in excel 95 format which allows you to do all kinds of formatting etc.

Anonymous said...

情趣用品情趣用品情趣


視訊聊天室辣妹視訊視訊美女視訊視訊交友網視訊聊天室視訊交友視訊美女免費視訊免費視訊聊天視訊交友90739免費視訊聊天室成人聊天室

視訊聊天


A片下載成人影片下載免費A片下載日本A片情色A片免費A片成人影城成人電影線上A片A片免費看


煙火影音視訊聊天室情趣用品情趣用品情趣用品

Unknown said...

I like the blog, thanks for giving the wonderful information.PHP Development

Shehroz Ahmed said...

Thanks for sharing us.
pandora locations

5689 said...

zzzzz2018.8.24
louboutin shoes
new nike shoes
nike huarache femme
ralph lauren uk
hermes belts
louboutin outlet
coach outlet
vibram fivefingers
nike air max 95
supreme shirt

Unknown said...

www1126



coach factory outlet
off white clothing
clarks shoes
ugg outlet
ecco shoes
kate spade
ed hardy uk
coach outlet online
true religion

pathiez said...

c6v77i1e87 e7s53z2g74 h4c51l7h54 h1k24r3r50 q4w57n1d42 f5m59z1b38