Skip to content

Instantly share code, notes, and snippets.

@anderson-mota
Created March 22, 2013 18:56
Show Gist options
  • Save anderson-mota/5223811 to your computer and use it in GitHub Desktop.
Save anderson-mota/5223811 to your computer and use it in GitHub Desktop.
Exporta para XLS
<?php
$result= array(
array('id'=>1, 'name'=>'teste 1', 'date'=>'2013-03-15'),
array('id'=>2, 'name'=>'teste 2', 'date'=>'2013-03-16'),
array('id'=>3, 'name'=>'teste 3', 'date'=>'2013-03-17'),
array('id'=>4, 'name'=>'teste 4', 'date'=>'2013-03-18'),
array('id'=>5, 'name'=>'teste 5', 'date'=>'2013-03-19'),
array('id'=>6, 'name'=>'teste 6', 'date'=>'2013-03-20'),
array('id'=>7, 'name'=>'teste 7', 'date'=>'2013-03-21'),
array('id'=>8, 'name'=>'teste 8', 'date'=>'2013-03-22'),
array('id'=>9, 'name'=>'teste 9', 'date'=>'2013-03-23'),
array('id'=>10, 'name'=>'teste 10', 'date'=>'2013-03-24')
);
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=list.xls");
header("Content-Transfer-Encoding: binary ");
xlsBOF();
xlsWriteLabel(0,0,"Heading1");
xlsWriteLabel(0,1,"Heading2");
xlsWriteLabel(0,2,"Heading3");
$xlsRow = 1;
foreach($result as $row)
{
xlsWriteNumber($xlsRow,0,$row['id']);
xlsWriteLabel($xlsRow,1,$row['name']);
xlsWriteLabel($xlsRow,2,$row['date']);
$xlsRow++;
}
xlsEOF();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment