TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   want to fetch data in excel sheet from my mysql database (http://www.talkphp.com/absolute-beginners/2994-want-fetch-data-excel-sheet-my-mysql-database.html)

sharma.9.pooja 06-23-2008 06:08 AM

want to fetch data in excel sheet from my mysql database
 
hi all,

i want to fetch my data from database in excel sheet.please anybody help me regarding this problem..
this is my code........

<?php
$local = "localhost";
$user = "root";
$pass = "root";
$db = "contact1";


$conn = mysql_connect($local,$user,$pass) or die ("Unable to connect to database.");

mysql_select_db($db, $conn);
$sql = mysql_query("SELECT * FROM submit");
$result = mysql_query($sql) or die('Error, query failed');
$tsv = array();
$html = array();
while($row = mysql_fetch_array($result))
{
$tsv[] = implode("\t", $row);
$html[] = "<tr><td>" .implode("</td><td>", $row) .<br>
"</td></tr>";
}

$tsv = implode("\r\n", $tsv);
echo $html = "<table>" . implode("\r\n", $html) . "</table>";

$fileName = 'mysql-to-excel.xls';
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$fileName");

//echo $tsv;
echo $html;
mysql_close($conn);

?>

:-/

sketchMedia 06-23-2008 02:45 PM

Why not do it to CSV, tested and this works for Open Office so it should work for excel (but microsoft being evil, it probably wont), there are also some bits of incorrect code within your example which i have fixed in mine:
PHP Code:

$conn mysql_connect($local,$user,$pass) or die ("Unable to connect to database.");

mysql_select_db($db$conn);

$result mysql_query("SELECT * FROM users") or die('Error, query failed');
$csv '';
$fileName 'test.csv';
while(
$row mysql_fetch_array($result))
{
    
$csv .= implode($row',')."\n";
}

header("Content-Type: text/comma-separated-values");
header("Content-Disposition: attachment; filename=$filename");
echo 
$csv

Hopefully that will give you what you need to get going.

sharma.9.pooja 06-24-2008 05:04 AM

hey,
really thankful for ur suggestion ....its wrking fine
but if we use the header type Content-type: application/vnd.ms-excel
we can easily save our data in .xls file and change the file name too

thanks pooja


All times are GMT. The time now is 06:06 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0