Ver Mensaje Individual
  #4 (permalink)  
Antiguo 04/01/2011, 12:02
PSPforever
 
Fecha de Ingreso: marzo-2008
Mensajes: 186
Antigüedad: 16 años, 1 mes
Puntos: 3
Respuesta: Exportar datos de un formulario a un EXCEL

Antes que nada chicos, gracias por vuestra ayuda, da gusto preguntar aquí.

Al final creo el excel utilizando el archivo agpform.php que lo he visto googleando y disponible para descarga. En resumen, formulario en el html, proceso con el archivo cagpform y se crea el excel en el directorio, pero aún me queda una duda.

Sólo comentar una duda que tengo para que en excel aparezcán las comillas, acentos,etc. y no sé dónde colocar el utf8_decode en el archivo agpform.php cuyo código publico debajo para la codificación de carácteres.

Código PHP:
Ver original
  1. <?php
  2. $success = "confirmacion.php";
  3.     $error = "error.php";
  4.    
  5.     // Change this to the character(s) you want to be placed instead of line breaks(new line, enter, etc)
  6.     $lbChar = " ";  // default is a space, you may change it to whatever you want
  7.    
  8.     // Don't change anything below this line       
  9.    
  10.     // Determine if the form was sent through the GET methog or the POST method.
  11.     if($_POST){
  12.         $array = $_POST;
  13.     } else if($_GET){          
  14.         $array = $_GET;
  15.     } else {
  16.             die("You must Access this file through a form.");   // If someone accesses the file directly, it wont work :)
  17.     }  
  18.  
  19.     //Check if the filename was sent through the form or not
  20.     if(!$array['filename']){
  21.         // if the filename wasnt sent through the form, it will become form.xls, you can change the default if you want.
  22.         $array['filename'] = "datos-registro.xls";  //Set the file to save the information in
  23.    
  24.     } else {
  25.         if(!(stristr($array['filename'],".xls"))){
  26.             $array['filename'] = $array['filename'] . ".xls";
  27.         }
  28.     }
  29.    
  30.     // Define the tab and carriage return characters:
  31.     $tab = "\t";    //chr(9);
  32.     $cr = "\n";     //chr(13);
  33.    
  34.     if($array){
  35.             // Make The Top row for the excel file and store it in the $header variable
  36.             $keys = array_keys($array);
  37.             foreach($keys as $key){
  38.                 if(strtolower($key) != 'filename' && strtolower($key) != 'title'){
  39.                     $header .= $key . $tab;
  40.                 }
  41.             }
  42.             $header .= $cr;
  43.            
  44.             //Make the line with the contents to write to the excel file.
  45.             foreach($keys as $key){
  46.                 if(strtolower($key) != 'filename' && strtolower($key) != 'title'){
  47.  
  48.                     $array[$key] = str_replace("\n",$lbChar,$array[$key]);
  49.                     $array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);
  50.                     $array[$key] = str_replace("\\","",$array[$key]);
  51.                     $array[$key] = str_replace($tab, "    ", $array[$key]);
  52.                     $data .= $array[$key] . $tab ;
  53.                 }
  54.             }
  55.             $data .= $cr;
  56.            
  57.             if (file_exists($array['filename'])) {
  58.                 $final_data = $data;        // If the file does exist, then only write the information the user sent
  59.             } else {
  60.                 $final_data = $header . $data;      // If file does not exist, write the header(first line in excel with titles) to the file
  61.             }
  62.             // open the file and write to it
  63.            
  64.             $fp = fopen($array['filename'],"a"); // $fp is now the file pointer to file $array['filename']
  65.            
  66.             if($fp){
  67.                
  68.                 fwrite($fp,$final_data);    //Write information to the file
  69.                 fclose($fp);        // Close the file
  70.                 // Success
  71.                 header("Location: $success");
  72.             } else {
  73.                 // Error
  74.                 header("Location: $error");
  75.             }
  76.     }
  77. ?>

Cita:
Yo suelo usar "php_writeexcel-0.3.0", viene con algunos ejemplos para colorear las celdas, el texto, etc.

P.D.: Un poquito tonto el cliente ¿no?.
Investigaré el que dices, porque seguramente me pedirán que se permita colorear celdas, etc.

Respecto al cliente, no es por hacer sangre, pero cada proyecto algunos clientes me lo ponen más complicado por la falta de cooperación que tienen y para escuchar recomendaciones.