Professional Documents
Culture Documents
01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Detalle del concepto</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="keywords" content="programacin, webmaster, tutorial">
<meta name="description" content="El objetivo de este sitio es ser un repositori
o de conceptos sobre php html xhtml css mysql javascript">
<link rel="StyleSheet" href="css/estilos.css" type="text/css">
<style type="text/css">
#contenido {
padding:10px;
background-color:#f0f0f0;
}
legend { font-family: sans-serif; padding:10px; }
fieldset {padding:10px;};
.titulo { font-family: arial; font-size:1.8em;}
.titulo2 { font-family: arial; font-size:1.4em; }
.comentarios { font-family: monospace; font-size:1.05em }
</style>
<script language="JavaScript">
function abrirVentana()
{
alto=(screen.availHeight/2)+100;
ancho=screen.availWidth-10;
window.open('', 'ventanaForm', 'top=0,left=0,width='+ancho+',height='+alto+',r
esizable=yes,scrollbars=yes,menubar=yes,status=yes,toolbar=yes,location=yes');
}
</script>
</head>
<body>
<div id="contenedor">
<div id="cabecera">
<h1><a href="index.php" class="enlaceindex">Cdigo fuente Ya.</a></h1>
</div>
<div id="subcabecera">
<p>Puede ejecutar la o las páginas para probar el concepto inmediatam
ente.</p>
<p> </p>
</div>
<div id="contenido">
<span class="titulo"><strong>Concepto:</strong>Exportar una tabla de mysql a Exc
el</span><br><br><span class="titulo2"><strong>El concepto trata sobre:</strong>
php,mysql</span><br><span class="titulo2"><strong>Autor:</strong></span><br><for
m method="post" action="http://www.problemasya.com.ar/ejecuta1.php" target="vent
anaForm" onsubmit="abrirVentana()"> <input type="submit" value="Ejecutar el p
rograma." class="botones">
<div>
<h2><a href="index.php?inicio=20">Retornar</a></h2>
</div>
<fieldset><legend><strong>Nombre del archivo:</strong>pagina1.php<br></legend><i
nput type="hidden" name="nomarchivo0" value="pagina1.php"><textarea name="archiv
o0" rows="10" cols="110"><?php
$conexion=mysql_connect("localhost","pepe","123456"
;) or
die("Problemas en la conexion");
mysql_select_db("codigofuenteya",$conexion) or
die("Problemas en la selección de la base de datos");
$registros=mysql_query("drop table if exists usuarios",$conexion) or
die("Problemas en el select:".mysql_error());
$registros=mysql_query("create table usuarios (
nombre varchar(30),
clave varchar(30)
)",$conexion) or
die("Problemas en el select:".mysql_error());
mysql_query("insert into usuarios(nombre,clave) values ('juan','123')"
,$conexion) or
die(mysql_error());
mysql_query("insert into usuarios(nombre,clave) values ('ana','abc')",
$conexion) or
die(mysql_error());
mysql_query("insert into usuarios(nombre,clave) values ('carlos','xyz')&quo
t;,$conexion) or
die(mysql_error());
$datos='';
$cabecera='';
$registros=mysql_query("select nombre,clave from usuarios",$conexion)
or
die(mysql_error());
$cant=mysql_num_fields($registros) or
die(mysql_error());
for($f=0;$f<$cant;$f++)
{
$cabecera.=mysql_field_name($registros,$f)."\t";
}
while($fila = mysql_fetch_row($registros))
{
$linea = '';
foreach($fila as $valor)
{
if ((!isset($valor)) || ($valor == ""))
{
$valor = "\t";
}
else
{
$valor = str_replace('"', '""', $valor);
$valor = '"' . $valor . '"' . "\t";
}
$valor = stripslashes($valor);
$linea .= $valor;
}
$datos .= trim($linea)."\n";
}
$datos = str_replace("\r","",$datos);
if ($datos == "")
{
$datos = "\n (0) registros encontrados!\n";
}
$nomarchivo='prueba1.xls';
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=$nomarchivo");
header("Pragma: no-cache");
header("Expires: 0");
print "$cabecera\n$datos";
?>
</textarea><br><strong>Comentarios</strong><div class="comentarios"><pre style="
">Creamos una tabla e insertamos como ejemplo varios registros:
$conexion=mysql_connect("localhost","pepe","123456"
;) or
die("Problemas en la conexion");
mysql_select_db("codigofuenteya",$conexion) or
die("Problemas en la selección de la base de datos");
$registros=mysql_query("drop table if exists usuarios",$conexion) or
die("Problemas en el select:".mysql_error());
$registros=mysql_query("create table usuarios (
nombre varchar(30),
clave varchar(30)
)",$conexion) or
die("Problemas en el select:".mysql_error());
mysql_query("insert into usuarios(nombre,clave) values ('juan','123')"
,$conexion) or
die(mysql_error());
mysql_query("insert into usuarios(nombre,clave) values ('ana','abc')",
$conexion) or
die(mysql_error());
mysql_query("insert into usuarios(nombre,clave) values ('carlos','xyz')&quo
t;,$conexion) or
die(mysql_error());
Recuperamos todos los registros y mediante un for recuperamos los nombres de los
campos:
for($f=0;$f<$cant;$f++)
{
$cabecera.=mysql_field_name($registros,$f)."\t";
}
Luego mediante un while y un for interno recuperamos todos los datos de cada fil
a de la tabla
y generamos un string con la información correspondiente:
while($fila = mysql_fetch_row($registros))
{
$linea = '';
foreach($fila as $valor)
{
if ((!isset($valor)) || ($valor == ""))
{
$valor = "\t";
}
else
{
$valor = str_replace('"', '""', $valor);
$valor = '"' . $valor . '"' . "\t";
}
$valor = stripslashes($valor);
$linea .= $valor;
}
$datos .= trim($linea)."\n";
}
Generamos una cabecera que indica que la información a suministrar debe s
er procesada por
una aplicación:
header("Content-type: application/x-msdownload");
</pre></div><script type="text/javascript"><!-google_ad_client = "pub-4669394804436935";
/* GRIS300x250, creado 16/03/08 */
google_ad_slot = "0299140816";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</fieldset> <input type="submit" value="Ejecutar el programa.
" class="botones">
<div>
<h2><a href="index.php?inicio=20">Retornar</a></h2>
</div>
<br>
<table width="650px">
<tr>
<td align="left">
</td>
</tr>
</table>
<br>
</div>
<div id="pie" style="text-align:center">
Cdigo Fuente Ya - 2015
</div>
</div>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-628756-16";
urchinTracker();
</script>
</body>
</html>