Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export to MSExcel Format 1

Status
Not open for further replies.

egmweb

Programmer
Mar 19, 2004
147
EU
Hello all.

I have been created a script to search into my database for retrieve certain info that I need.

But I need to export the result of the query to an MS Excel file.

Would you please tell me hoe can I do that?

Thanks all..

Eduard
 
export to csv, thats possibly the easiest format to get excel to read.

Otherwise its going to require (probably) both of these:
a) bashing programming manuals on COM (you're on your own pretty much here, I along with many others gave up).
b) having a M$ powered server with Excel installed.

I don't think theres actually any other way to do this without a) or b)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thanks you Karver, but do you know any article, or something that can guide me to make the script??? or maybe an script already made?

Thanks you.
 
The following code is called from a form submitting the query to save.php, its old code so the variables aren't set using $_POST or $_GET, you may need to change these.
It basically dumps the dats to a download stream offering the option too save or open (c/o your browser seeing whats coming).


save.php
Code:
<?
//configure the database here, or pass it along with $sql (the query) and $query (the value used to name the file).
$db="database";

// I use a config.inc.php with the databse connection details, include one here if required
//require('config.inc.php');


if(!isset($sql)){
  echo "No Query";
  exit;
	}else{
	$sql=stripslashes($sql);
	$file=$query.".csv";
}

header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment;filename=".$file );
header('Pragma: no-cache');
header('Expires: 0');

mysql_select_db($db,$connection);
$result = mysql_query($sql,$connection);
if($result){


	$columns=@mysql_num_fields($result);
for ($i = 0; $i < mysql_num_fields($result); $i++) {

	print "\"".mysql_field_name($result,$i)."\",";

}
echo "\n";
	
while ($myrow = mysql_fetch_array($result)){
	for ($i = 0; $i < ($columns); $i++) {
		echo "\"".$myrow[$i]."\",";
	}
		echo "\n";
}
}else{
echo "No results";
}

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Hello KarveR, THANKS YOU VERY MUCH... IT WORKSSSS

Best Regards.

Eduard!!
 
another way is to just use HTML tags
ie
Code:
<table border="1">
<tr>
<td>column 1</td>
<td>column 2</td>
</tr>
</table>
This will create a table in excel using the table's TR as rows and TD as cells just like in html.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top