Is there anything missing in my code while exporting to excel from php
Question by sujal
My function
function xlstest(){
$headers='SN'."t".'Reservation ID'."t".'Hotel Name'."t".'Customer Name';
$data='1'."t".'234'."t".'My hotel name'."t".'Jonny';
$filename='testing';
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$filename.".xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "$headersn$data";
}
while calling this function in admin panel it will export to excel but file doesn’t have above headers and data but it shows all the text that is present in admin panel
Do anyone know why is this happening?
Answer by sujal
My problem is solved. Actually I have called this function just below the <link rel="stylesheet" href=""/>
and
<script type="text/javascript" src=".... .."></script>
once i moved above these css and scripts.. my problem is solved strange…….
Answer by Starx
Your are not using phpexcel api at all. Please read the documentation and try to use the api.
And about the codes you are using, separate the field using t
(with spaces back and front).
Also, instead of octet-stream
use excels header file.
header('Content-type: application/ms-excel');
Usage:
function xlstest(){
$headers='SN'." t ".'Reservation ID'." t ".'Hotel Name'." t ".'Customer Name';
$data='1'." t ".'234'." t ".'My hotel name'." t ".'Jonny';
$filename='testing';
header("Content-type: application/ms-excel");
header("Content-Disposition: attachment; filename=".$filename.".xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "$headersn$data";
}
Update:
To export a CSV file you can do do the following. Its almost same, but the delimiter is a comma instead and, the content-type is application/vnd.ms-excel
.
function csvtest(){
$headers='SN'.",".'Reservation ID'.",".'Hotel Name'.",".'Customer Name';
$data='1'.",".'234'.",".'My hotel name'.",".'Jonny';
$filename='testing';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$filename.".csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "$headersn$data";
}