Foros del Web » Programando para Internet » PHP »

Exportar a Excel tabla mysql

Estas en el tema de Exportar a Excel tabla mysql en el foro de PHP en Foros del Web. Buenos dias, Utilizo el siguiente código para que en la página me aparezca una tabla de la base de datos. Tienen un código para que ...
  #1 (permalink)  
Antiguo 24/01/2013, 07:59
 
Fecha de Ingreso: enero-2013
Mensajes: 64
Antigüedad: 11 años, 3 meses
Puntos: 0
Exportar a Excel tabla mysql

Buenos dias,

Utilizo el siguiente código para que en la página me aparezca una tabla de la base de datos. Tienen un código para que me exporta a Excel, pero lo que estoy queriendo lograr es que mediante un link me exporte; ya que utilizando el código que figura abajo, al ingresar ya me exporta.

Código PHP:
<?php
error_reporting
(E_ERROR);
require(
"conexion.php");
require(
"funciones.php");
header("Content-type: application/vnd.ms-excel" ) ; //Exporar a Excel
header("Content-Disposition: attachment; filename=estadisticas.xls" ) ; //Exporar a Excel

//.sqlValue($criterio1."%", "text")

    
$query "SELECT * FROM bd_rtdmf WHERE ID_N IN ('000')";
    
$queEmp mysql_query($query$conexion);
    
$campos mysql_num_fields($query) ; 

$i=0//Exporar a Excel
echo "<table><tr>"
while(
$i<$campos){ 
echo 
"<td>"mysql_field_name ($query$i) ; 
echo 
"</td>"
$i++; 

echo 
"</tr>"
while(
$row=mysql_fetch_array($query)){ 
echo 
"<tr>"
for(
$j=0$j<$campos$j++) { 
echo 
"<td>".$row[$j]."</td>"

echo 
"</tr>"

echo 
"</table>"
    

?>
Muchas gracias por la lectura, saludos.
  #2 (permalink)  
Antiguo 24/01/2013, 08:01
Avatar de andres_15_  
Fecha de Ingreso: septiembre-2008
Ubicación: Cali
Mensajes: 232
Antigüedad: 15 años, 7 meses
Puntos: 7
Respuesta: Exportar a Excel tabla mysql

Cita:
Iniciado por facundojr07 Ver Mensaje
Buenos dias,

Utilizo el siguiente código para que en la página me aparezca una tabla de la base de datos. Tienen un código para que me exporta a Excel, pero lo que estoy queriendo lograr es que mediante un link me exporte; ya que utilizando el código que figura abajo, al ingresar ya me exporta.

Código PHP:
<?php
error_reporting
(E_ERROR);
require(
"conexion.php");
require(
"funciones.php");
header("Content-type: application/vnd.ms-excel" ) ; //Exporar a Excel
header("Content-Disposition: attachment; filename=estadisticas.xls" ) ; //Exporar a Excel

//.sqlValue($criterio1."%", "text")

    
$query "SELECT * FROM bd_rtdmf WHERE ID_N IN ('000')";
    
$queEmp mysql_query($query$conexion);
    
$campos mysql_num_fields($query) ; 

$i=0//Exporar a Excel
echo "<table><tr>"
while(
$i<$campos){ 
echo 
"<td>"mysql_field_name ($query$i) ; 
echo 
"</td>"
$i++; 

echo 
"</tr>"
while(
$row=mysql_fetch_array($query)){ 
echo 
"<tr>"
for(
$j=0$j<$campos$j++) { 
echo 
"<td>".$row[$j]."</td>"

echo 
"</tr>"

echo 
"</table>"
    

?>
Muchas gracias por la lectura, saludos.
pues no soy experto, pero si tal vez intentas colocando un form action="Nombredelarchivo" desde la pagina de donde quieres que se exporte?
  #3 (permalink)  
Antiguo 24/01/2013, 08:07
 
Fecha de Ingreso: enero-2013
Mensajes: 64
Antigüedad: 11 años, 3 meses
Puntos: 0
Respuesta: Exportar a Excel tabla mysql

Jaja, yo menos soy experto. Estoy haciendo esto porque en mi trabajo necesitan. A ver, voy a tratar de entender lo que me respondiste. Gracias.
  #4 (permalink)  
Antiguo 24/01/2013, 08:16
Avatar de andres_15_  
Fecha de Ingreso: septiembre-2008
Ubicación: Cali
Mensajes: 232
Antigüedad: 15 años, 7 meses
Puntos: 7
Respuesta: Exportar a Excel tabla mysql

Cita:
Iniciado por facundojr07 Ver Mensaje
Jaja, yo menos soy experto. Estoy haciendo esto porque en mi trabajo necesitan. A ver, voy a tratar de entender lo que me respondiste. Gracias.
, es decir me imagino que estas haciendo un formulario y despues insertaste los datos mediante un php y todo esto... Pues lo que digo es que si hiciste el formulario para mandar los datos al php debiste de haber creado uno como estos
Código HTML:
<input type="submit" value="Enviar"
y al hacer eso envias los datos al php. Pues lo que te digo es que hagas lo mismo haciendo esto

Código HTML:
<form action="nombredelarchivoexcell"></form> 
y dentro de la etiqueta form esto:
Código HTML:
<input type="submit" value="Generar Excell"
Ojala y te sirva
  #5 (permalink)  
Antiguo 24/01/2013, 08:24
 
Fecha de Ingreso: enero-2013
Mensajes: 64
Antigüedad: 11 años, 3 meses
Puntos: 0
Respuesta: Exportar a Excel tabla mysql

Código PHP:
<?php
error_reporting
(E_ERROR);
require(
"conexion.php");
require(
"funciones.php");
header("Content-type: application/vnd.ms-excel" ) ; //Exporar a Excel
header("Content-Disposition: attachment; filename=estadisticas.xls" ) ; //Exporar a Excel

//.sqlValue($criterio1."%", "text")

    
$query "SELECT * FROM bd_rtdmf WHERE ID_N IN ('000')";
    
$queEmp mysql_query($query$conexion);
    
$campos mysql_num_fields($query) ; 

$i=0//Exporar a Excel
echo "<table><tr>"
while(
$i<$campos){ 
echo 
"<td>"mysql_field_name ($query$i) ; 
echo 
"</td>"
$i++; 

echo 
"</tr>"
while(
$row=mysql_fetch_array($query)){ 
echo 
"<tr>"
for(
$j=0$j<$campos$j++) { 
echo 
"<td>".$row[$j]."</td>"

echo 
"</tr>"

echo 
"</table>"

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Red Banda Ancha Fija</title>

<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="Silverlight Binaries/Visifire.js"></script>

</head>
<body>
<table width="100%" border="0">
  <tr>
    <td>
          <div align="center"><?php
    
include('menu-rtdmf4.php');
?>
      </div>
    </td>
  </tr>
  <tr>
    <td><p><span class="TITULO">000</span></p>
      <table width="100%" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <th><div align="center">Mes</div></th>
        <th><div align="center">Trafico</div></th>
        <th><div align="center">Intentos</div></th>
        <th><div align="center">Tomas</div></th>
        <th><div align="center">Contestadas</div></th>
        <th><div align="center">Desborde</div></th>
        <th><div align="center">% TTR </div></th>
        <th><div align="center">% Desborde </div></th>
      </tr>
      <?php while ($rsEmp mysql_fetch_assoc($queEmp)) { ?>
      <tr>
        <td><div align="center"><?php echo $rsEmp['Nombre_mes']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Trafico']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Intentos']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Tomas']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Contestadas']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Desborde']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['TTR']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Desborde1']; ?></div></td>
      </tr>
      <?php ?>
    </table></td>
  </tr>
</table>
<table width="100%" border="0">
  <tr>
    <td width="50%" valign="top"><div align="center">
      <iframe id="grafico1" name="grafico1" src="pag-rtdmf-000-grafico1.php" frameborder="0" framespacing="0" scrolling="No" border="0" width="525"; height="325"> Necesita que su navegador soporte iframe para ver el gr&aacute;fico. Muchas gracias. </iframe>
    </div></td>
    <td width="50%"><div align="center">
      <iframe id="grafico2" name="grafico2" src="pag-rtdmf-000-grafico2.php" frameborder="0" framespacing="0" scrolling="No" border="0" width="525"; height="325"> Necesita que su navegador soporte iframe para ver el gr&aacute;fico. Muchas gracias. </iframe>
    </div></td>
  </tr>
</table>
</body>
</html>
Esa es la pagína. Donde muestra una tabla desde una base de datos. Lo que estoy queriendo hacer es que mediante un link me exporte esa tabla de mysql a excel. Gracias por tus respuestas.
  #6 (permalink)  
Antiguo 24/01/2013, 09:15
Avatar de andres_15_  
Fecha de Ingreso: septiembre-2008
Ubicación: Cali
Mensajes: 232
Antigüedad: 15 años, 7 meses
Puntos: 7
Respuesta: Exportar a Excel tabla mysql

Cita:
Iniciado por facundojr07 Ver Mensaje
Código PHP:
<?php
error_reporting
(E_ERROR);
require(
"conexion.php");
require(
"funciones.php");
header("Content-type: application/vnd.ms-excel" ) ; //Exporar a Excel
header("Content-Disposition: attachment; filename=estadisticas.xls" ) ; //Exporar a Excel

//.sqlValue($criterio1."%", "text")

    
$query "SELECT * FROM bd_rtdmf WHERE ID_N IN ('000')";
    
$queEmp mysql_query($query$conexion);
    
$campos mysql_num_fields($query) ; 

$i=0//Exporar a Excel
echo "<table><tr>"
while(
$i<$campos){ 
echo 
"<td>"mysql_field_name ($query$i) ; 
echo 
"</td>"
$i++; 

echo 
"</tr>"
while(
$row=mysql_fetch_array($query)){ 
echo 
"<tr>"
for(
$j=0$j<$campos$j++) { 
echo 
"<td>".$row[$j]."</td>"

echo 
"</tr>"

echo 
"</table>"

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Red Banda Ancha Fija</title>

<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="Silverlight Binaries/Visifire.js"></script>

</head>
<body>
<table width="100%" border="0">
  <tr>
    <td>
          <div align="center"><?php
    
include('menu-rtdmf4.php');
?>
      </div>
    </td>
  </tr>
  <tr>
    <td><p><span class="TITULO">000</span></p>
      <table width="100%" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <th><div align="center">Mes</div></th>
        <th><div align="center">Trafico</div></th>
        <th><div align="center">Intentos</div></th>
        <th><div align="center">Tomas</div></th>
        <th><div align="center">Contestadas</div></th>
        <th><div align="center">Desborde</div></th>
        <th><div align="center">% TTR </div></th>
        <th><div align="center">% Desborde </div></th>
      </tr>
      <?php while ($rsEmp mysql_fetch_assoc($queEmp)) { ?>
      <tr>
        <td><div align="center"><?php echo $rsEmp['Nombre_mes']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Trafico']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Intentos']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Tomas']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Contestadas']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Desborde']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['TTR']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Desborde1']; ?></div></td>
      </tr>
      <?php ?>
    </table></td>
  </tr>
</table>
<table width="100%" border="0">
  <tr>
    <td width="50%" valign="top"><div align="center">
      <iframe id="grafico1" name="grafico1" src="pag-rtdmf-000-grafico1.php" frameborder="0" framespacing="0" scrolling="No" border="0" width="525"; height="325"> Necesita que su navegador soporte iframe para ver el gr&aacute;fico. Muchas gracias. </iframe>
    </div></td>
    <td width="50%"><div align="center">
      <iframe id="grafico2" name="grafico2" src="pag-rtdmf-000-grafico2.php" frameborder="0" framespacing="0" scrolling="No" border="0" width="525"; height="325"> Necesita que su navegador soporte iframe para ver el gr&aacute;fico. Muchas gracias. </iframe>
    </div></td>
  </tr>
</table>
</body>
</html>
Esa es la pagína. Donde muestra una tabla desde una base de datos. Lo que estoy queriendo hacer es que mediante un link me exporte esa tabla de mysql a excel. Gracias por tus respuestas.
Podrias hacer esto (antes que todo Quitar Las etiquetas que te crean el archivo excell, para que solo te muestre la tabla) :

[PHP][QUOTE=facundojr07;4354680]
Código PHP:
<?php
/*error_reporting(E_ERROR);
require("conexion.php");
require("funciones.php");
header("Content-type: application/vnd.ms-excel" ) ; //Exporar a Excel
header("Content-Disposition: attachment; filename=estadisticas.xls" ) ; //Exporar a Excel*/ 
Esto es lo que deberias quitarle

//.sqlValue($criterio1."%", "text")

    
$query "SELECT * FROM bd_rtdmf WHERE ID_N IN ('000')";
    
$queEmp mysql_query($query$conexion);
    
$campos mysql_num_fields($query) ; 

$i=0//Exporar a Excel
echo "<table><tr>"
while(
$i<$campos){ 
echo 
"<td>"mysql_field_name ($query$i) ; 
echo 
"</td>"
$i++; 

echo 
"</tr>"
while(
$row=mysql_fetch_array($query)){ 
echo 
"<tr>"
for(
$j=0$j<$campos$j++) { 
echo 
"<td>".$row[$j]."</td>"

echo 
"</tr>"

echo 
"</table>"

//al final de la consulta imprimes esto

echo "
<form action="" method="
POST"> 
            <input type='reset' value='Generar Archivo Excell'> 
</form>
            "
;

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Red Banda Ancha Fija</title>

<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="Silverlight Binaries/Visifire.js"></script>

</head>
<body>
<table width="100%" border="0">
  <tr>
    <td>
          <div align="center"><?php
    
include('menu-rtdmf4.php');


 

?>
Creo que te seriviria, pruebale y cuentame como te fue
  #7 (permalink)  
Antiguo 24/01/2013, 09:31
 
Fecha de Ingreso: enero-2013
Mensajes: 64
Antigüedad: 11 años, 3 meses
Puntos: 0
Respuesta: Exportar a Excel tabla mysql

Creo que no nos estamos entendiendo. Porque el codigo que me enviaste vos es para que lo exporte a excel directamente, el mismo no lo probé.
Y lo que yo estoy necesitando, que no se como hacerlo es que el siguiente codigo, que es la página

Código PHP:
<?php
error_reporting
(E_ERROR);
require(
"conexion.php");
require(
"funciones.php");


//.sqlValue($criterio1."%", "text")

    
$query "SELECT * FROM bd_rtdmf WHERE ID_N IN ('000')";
    
$queEmp mysql_query($query$conexion);
    

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Red Banda Ancha Fija</title>

<link href="styles.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="Silverlight Binaries/Visifire.js"></script>

</head>
<body>
<table width="100%" border="0">
  <tr>
    <td>
          <div align="center"><?php
    
include('menu-rtdmf4.php');
?>
      </div>
    </td>
  </tr>
  <tr>
    <td><p><span class="TITULO">000</span></p>
      <table width="100%" border="1" cellpadding="0" cellspacing="0">
      <tr>
        <th><div align="center">Mes</div></th>
        <th><div align="center">Trafico</div></th>
        <th><div align="center">Intentos</div></th>
        <th><div align="center">Tomas</div></th>
        <th><div align="center">Contestadas</div></th>
        <th><div align="center">Desborde</div></th>
        <th><div align="center">% TTR </div></th>
        <th><div align="center">% Desborde </div></th>
      </tr>
      <?php while ($rsEmp mysql_fetch_assoc($queEmp)) { ?>
      <tr>
        <td><div align="center"><?php echo $rsEmp['Nombre_mes']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Trafico']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Intentos']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Tomas']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Contestadas']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Desborde']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['TTR']; ?></div></td>
        <td><div align="center"><?php echo $rsEmp['Desborde1']; ?></div></td>
      </tr>
      <?php ?>
    </table></td>
  </tr>
</table>
</body>
</html>
Agregarle un link para que esos datos me importen a excel. En las respuestas anteriores figura el codigo que le agregue, pero lo que hace es que al abrir la pagina directamente me descarga el xls. Igual muchas gracias. Creo que ya encontre la solución, voy a intentarlo y te aviso.

Última edición por facundojr07; 24/01/2013 a las 09:37
  #8 (permalink)  
Antiguo 24/01/2013, 11:04
 
Fecha de Ingreso: enero-2013
Mensajes: 64
Antigüedad: 11 años, 3 meses
Puntos: 0
Respuesta: Exportar a Excel tabla mysql

Ahí pude... lo que hice es crear un codigo nuevo en otra pagina

Código PHP:

<?php 
header
("Content-type: application/vnd.ms-excel" ) ; 
header("Content-Disposition: attachment; filename=estadisticas.xls" ) ; 
$servidor="localhost"
$user="root"
$pass=""
$db="bd_XXXX"
$tabla="bd_rtdmf"
mysql_connect($servidor,$user,$pass) ; 
mysql_select_db($db) ;

if(isset(
$_GET['df'])){
        
$DF $_GET['df'];
    }
$query=mysql_query("SELECT * FROM bd_rtdmf WHERE ID_N IN ('".$DF."')" ) ; 
$campos mysql_num_fields($query) ; 

$i=0
echo 
"<table><tr>"
while(
$i<$campos){ 
echo 
"<td>"mysql_field_name ($query$i) ; 
echo 
"</td>"
$i++; 

echo 
"</tr>"
while(
$row=mysql_fetch_array($query)){ 
echo 
"<tr>"
for(
$j=0$j<$campos$j++) { 
echo 
"<td>".$row[$j]."</td>"

echo 
"</tr>"

echo 
"</table>";

?>
Y en otra página, donde va en link agregue

Código PHP:
<?php echo "<a href='pag-mysql-excel.php?df=000'>Exportar a Excel</a><br />"?>
Y me funciona OK. Gracias por intentar ayudarme. Saludos.

Etiquetas: excel, mysql, select, sql, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:57.