25/05/2011

[PHP] Create XLS document

If you need to create XLS documents via PHP, you may need these functions:

//XLS Begin of file
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
//XLS End of file
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
//Writes a number in a cell
function xlsWriteNumber($Row, $Col, $Value) {
if($Value == null){
xlsWriteLabel($Row, $Col, $Value);
}
else{
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
}
return;
}
//Writes a string in a cell
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}

//Creates headers to download the file
function xlsWriteHeader(){
// 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=EXPORTNAME.xls ");
header("Content-Transfer-Encoding: binary ");
}

No comments:

Post a Comment

With great power comes great responsibility