PHP - Generacion Archivo Plano

 
Vista:

Generacion Archivo Plano

Publicado por Jaime Betancur Espinosa (4 intervenciones) el 21/12/2006 15:04:15
Hola a Todos, mi pregunta es la siguiente:

¿Alguien sabe como puedo crear un archivo plano, un .txt o un archivo de excel a partir de una consulta en MySql y Php?

Cualquier colaboración estaré muy agradecido

Muchas Gracias, espero me puedan ayudar
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Generacion Archivo Plano

Publicado por Juan Agote (5 intervenciones) el 22/12/2006 13:19:11
Este codigo te genera un excel:

<?php
include('conexion.php');

$cliente = getParametro("cliente");
$sql_select ="select p.nombre as proyecto,c.nombre as cliente,u.nombre as empleado,h.horas,h.fecha,h.id_horas from horas h,proyecto p,cliente c,usuario u where h.cliente_id_cliente =" .$cliente.
" and c.id_cliente = h.cliente_id_cliente and p.id_proyecto = h.proyecto_id_proyecto and u.id_usuario = h.usuario_id_usuario";
//die ($sql_select);
$result = mysql_query($sql_select, $conexion);
$count = mysql_num_fields($result);


for ($i = 0; $i < $count; $i++){
$header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result)){
$line = '';
foreach($row as $value){
if(!isset($value) || $value == ""){
$value = "\t";
}else{
# important to escape any quotes to preserve them in the data.
$value = str_replace('"', '""', $value);
# needed to encapsulate data in quotes because some data might be multi line.
# the good news is that numbers remain numbers in Excel even though quoted.
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data .= trim($line)."\n";
}
# this line is needed because returns embedded in the data have "\r"
# and this looks like a "box character" in Excel
$data = str_replace("\r", "", $data);

# Nice to let someone know that the search came up empty.
# Otherwise only the column name headers will be output to Excel.
if ($data == "") {
$data = "\nno matching records found\n";
}

# This line will stream the file to the user rather than spray it across the screen
header("Content-type: application/octet-stream");

# replace excelfile.xls with whatever you want the filename to default to
header("Content-Disposition: attachment; filename=InformeClientes.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;
?>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar