Forum Moderators: coopster

Message Too Old, No Replies

Export query to excel file

         

omoutop

12:31 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi to all..

Ihave a query to display all the unique email address from a specific table....
/////////////query////////////////
$url2 = "select distinct(Email) as Email from reservations where Island='$destination' order by Email";
$resulturl2 = mysql_query($url2);
while ($resurl2=mysql_fetch_assoc($resulturl2))
{
$email = $resurl2['Email'];

?>
<strong><? echo $email?></strong> <br>
<?
}
?>
//////////////////////////////

I would like to be able to export the results in an excel file somehow, each email should go to a different row of the spreadsheet.....and if this can be done...where does the excel file is saved?

Plz help me, any comments will be appreciated...

jatar_k

3:56 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could generate a csv file, that will be able to be opened in excel.

you could also try this
[webmasterworld.com...] msg 9

though coop worked out some header issues with that function, I can't remember where that is though.

>> where does the excel file is saved

that particular bit of code will prompt for save I think

it depends on where you want to save it, as a file on the server or on a local machine. Both options are fairly straight forward.

coopster

4:06 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sites setup to use Content Negotiation will find that MSIE has issues when data is pushed to the browser in those otherwise normal fashion. MSIE chokes and pukes because it doesn't obey the standards practices. I still haven' done a more advanced write-up on it, but the solution is here:

Export a table to CSV [webmasterworld.com]

RonPK

4:28 pm on Sep 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



omoutop, if you want to avoid the use of CSV, simply put your data into an HTML table. Use <th> for column headers. Your output could look like this:
<table> 
<tr>
<th>email</th>
</tr>
<tr>
<td>one@example.com</td>
</tr>
<tr>
<td>two@example.com</td>
</tr>
</table>

Give the output the extension .xls. MS Excel will open such a file without any conversion prompts.

Send the headers mentioned in the other posts, or try this, which usually works fine for me:

header("Content-type: application/vnd.ms-excel"); 
header("Content-Disposition: attachment; filename=somename.xls");
header("Content-length: ". strlen($data));
header("Content-Transfer-Encoding: BINARY");

omoutop

6:23 am on Sep 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thx all of you for the replies..I will try to figure it out and let u know....
Thx again!

omoutop

7:39 am on Sep 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thx all of you!..
this bit of code did everything for me...

///////////////////////////////////////////////
$destination = $_REQUEST['destination'];
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Emails.xls");

header("Content-Transfer-Encoding: BINARY");
include "dbconnect.php";
?>
<table>

<?
$url2 = "select distinct(Email) as Email from reservations where Island='$destination' order by Email";
$resulturl2 = mysql_query($url2);
while ($resurl2=mysql_fetch_assoc($resulturl2))
{
$email = $resurl2['Email'];
?>

<tr>
<? echo $email?>
</tr>
<?
}
?>
</table>
//////////////////////////////////////
thx again