May 14, 2012

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
enter image description here

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 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";
}
March 24, 2012

PHP Excel Header

Question by user794624

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-type:   application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xsl"); 
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo "Some Text"

Here is code to write and download xsl file using php,
my problem is when i open excel file MS-Excel show warning before opening file says

The file you are trying to open is in different format than specified by the file extension…Blah blah

What’s to do with PHP code to remove this warning. Contents are written correctly.

i know this is because content written in file are txt file content and file extension is incorrect, that is, xls. Solution???

Please don’t suggest to use any library.

Answer by Starx

You are giving multiple Content-Type headers. application/vnd.ms-excel is enough.

And there are couple of syntax error too. To statement termination with ; on the echo statement and wrong filename extension.

header("Content-Type:   application/vnd.ms-excel; charset=utf-8");
header("Content-Disposition: attachment; filename=abc.xls");  //File name extension was wrong
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
echo "Some Text"; //no ending ; here
...

Please fill the form - I will response as fast as I can!