We have all wanted data to be transfered to excel at one stage or another. I would like to know how people have done it and come to a general consensis on how it should be done. I usually use fopen or simple echo statement and declaring it as .xls or .csv when required. I have found a new error which brings up a NON EXCEL format and a SYLK error (due to me using sessions in the same file I am downloading, which I dont know why it matters). But if I wish to remove both errors as it does not look professional. Also I would like to know the offical content type for both excel formats (.xls and .xlsx). Add your comments and self code so we can get a general consensis!!!
My code:
session_start();
$filename = "test.xls";
$ABC = $_SESSION['A']; // Yes a valid session from previous page
$line_1 = "A" . "\t" . "B" . "\t" . "C";
$line_2 = "1" . "\t" . "2" . "\t" . "3";
$line_3 = "HELLO " . $ABC;
$data = $line_1 . "\n" . $line_2 . "\n" . $line_3;
session_destroy();
// What I find a mystery is what is the OFFICAL content type for excel?
header("Content-Type: application/x-msexcel");
header("Content-Disposition: attachment; filename=" . $filename);
echo $data;
Regards, X