Re:X [PHP-ES] Pasar a excel
| From: | samuel i. azcarraga velazquez | Date: | Fri, 15 Aug 2003 15:01:57 +0000 |
| Subject: | Re:X [PHP-ES] Pasar a excel | ||
| References: | 1 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-39426@lists.php.net to get a copy of this message | ||
aqui tienes un ejemplo de como hacerlo
esto te da la opcion de guardar o abrir el archivo
desde el navegador.
espero te funcione.
eso es todo un saludo.
<?
$DB_Server = "localhost"; //your MySQL Server
$DB_Username = "root"; //your MySQL User Name
$DB_Password = ""; //your MySQL Password
$DB_DBName = "horcon"; //your MySQL Database Name
$DB_TBLName = "ventas"; //your MySQL Table Name
$sql = "Select * from $DB_TBLName";
$Use_Title = 1;
$now_date = date('m-d-Y H:i');
$title = "Dump For Table $DB_TBLName from Database
$DB_DBName on $now_date";
$Connect = @mysql_connect($DB_Server, $DB_Username,
$DB_Password)
or die("Couldn't connect to MySQL:<br>" .
mysql_error() . "<br>" . mysql_errno());
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:<br>" .
mysql_error(). "<br>" . mysql_errno());
$result = @mysql_query($sql,$Connect)
or die("Couldn't execute query:<br>" .
mysql_error(). "<br>" . mysql_errno());
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment;
filename=reporte.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
//define separator (defines columns in excel & tabs in
word)
$sep = "\t"; //tabbed character
//start of printing column names as names of MySQL
fields
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "\t";
}
print("\n");
//end of printing column names
//start while loop to get data
/*
note: the following while-loop was taken from
phpMyAdmin 2.1.0.
--from the file "lib.inc.php".
*/
while($row = mysql_fetch_row($result))
{
//set_time_limit(60); // HaRa
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "",
$schema_insert);
//following fix suggested by Josue (thanks, Josue!)
//this corrects output in excel when table fields
contain \n or \r
//these two characters are now replaced with a space
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", "
", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";
}
?>
_________________________________________________________
Do You Yahoo!?
Información de Estados Unidos y América Latina, en Yahoo! Noticias.
Visítanos en http://noticias.espanol.yahoo.com