Codigo PHP para exportar formulario HTML a excel
Publicado por Jordi (2 intervenciones) el 14/09/2018 16:04:55
Salutaciones,
Tengo un mini formulario HTML que necesitaría que los datos automáticamente se exportaran a un archivo Excel al pulsar el botón “Submit” pero no me termina de funcionar.
Tengo un código PHP que en principio tendría que crear el archivo XLS si no está creado, y rellenar los campos con los valores entrados en el formulario HTML. En principio el archivo XLS se crea correctamente, la primera fila corresponde a titulo de los valores, pero no me rellena los valores entrados en el formulario. Y en vez de ir a la pagina de Error.html para indicar que algo ha ido mal me salta a la pagina de ok.html como si todo hubiera ido bien.
Alguien me podría echar una mano por favor.
Muchas gracias!
El código es el siguiente:
Tengo un mini formulario HTML que necesitaría que los datos automáticamente se exportaran a un archivo Excel al pulsar el botón “Submit” pero no me termina de funcionar.
Tengo un código PHP que en principio tendría que crear el archivo XLS si no está creado, y rellenar los campos con los valores entrados en el formulario HTML. En principio el archivo XLS se crea correctamente, la primera fila corresponde a titulo de los valores, pero no me rellena los valores entrados en el formulario. Y en vez de ir a la pagina de Error.html para indicar que algo ha ido mal me salta a la pagina de ok.html como si todo hubiera ido bien.
Alguien me podría echar una mano por favor.
Muchas gracias!
El código es el siguiente:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
$success = "ok.html";
$error = "error.html";
// Change this to the character(s) you want to be placed instead of line breaks(new line, enter, etc)
$lbChar = " "; // default is a space, you may change it to whatever you want
// Don't change anything below this line
// Determine if the form was sent through the GET methog or the POST method.
if($_POST){
$array = $_POST;
} else if($_GET){
$array = $_GET;
} else {
die("You must Access this file through a form."); // If someone accesses the file directly, it wont work :)
}
//Check if the filename was sent through the form or not
if(!$array['filename']){
// if the filename wasnt sent through the form, it will become form.xls, you can change the default if you want.
$array['filename'] = "form.xls"; //Set the file to save the information in
} else {
if(!(stristr($array['filename'],".xls"))){
$array['filename'] = $array['filename'] . ".xls";
}
}
// Define the tab and carriage return characters:
$tab = "\t"; //chr(9);
$cr = "\n"; //chr(13);
if($array){
// Make The Top row for the excel file and store it in the $header variable
$keys = array_keys($array);
foreach($keys as $key){
if(strtolower($key) != 'filename' && strtolower($key) != 'title'){
$header .= $key . $tab;
}
}
$header .= $cr;
//Make the line with the contents to write to the excel file.
foreach($keys as $key){
if(strtolower($key) != 'filename' && strtolower($key) != 'title'){
$array[$key] = str_replace("\n",$lbChar,$array[$key]);
$array[$key] = preg_replace('/([\r\n])/e',"ord('$1')==10?'':''",$array[$key]);
$array[$key] = str_replace("\\","",$array[$key]);
$array[$key] = str_replace($tab, " ", $array[$key]);
$data .= $array[$key] . $tab ;
}
}
$data .= $cr;
if (file_exists($array['filename'])) {
$final_data = $data; // If the file does exist, then only write the information the user sent
} else {
$final_data = $header . $data; // If file does not exist, write the header(first line in excel with titles) to the file
}
// open the file and write to it
$fp = fopen($array['filename'],"a"); // $fp is now the file pointer to file $array['filename']
if($fp){
fwrite($fp,$final_data); //Write information to the file
fclose($fp); // Close the file
// Success
header("Location: $success");
} else {
// Error
header("Location: $error");
}
}
?>
Valora esta pregunta
0