PHP - Cargar Excel con 3 hojas a sus respectivas 3 tablas

 
Vista:
sin imagen de perfil

Cargar Excel con 3 hojas a sus respectivas 3 tablas

Publicado por Leo (1 intervención) el 17/05/2017 16:17:22
Estimados, los molesto para ver si me pueden ayudar con este tema, tengo un archivo excel el cual se descarga de una aplicacion que tiene 3 hojas, lo que necesito es subir cada hoja a un tabla distinta, soy nuevo PHP, solo logre subir todo el archivo excel a una sola tabla, por lo que me quedo todo mezclado:

Les dejo el codigo que estoy usando:
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
$connect = mysqli_connect("localhost", "root", "", "tabla");
$output = '';
if(isset($_POST["import"]))
{
 $exp =(explode(".", $_FILES["excel"]["name"])); // For getting Extension of selected file
 $extension = end($exp);
 $allowed_extension = array("xls", "xlsx", "csv"); //allowed extension
 
 
 
 
 if(in_array($extension, $allowed_extension)) //check selected file extension is present in allowed extension array
 {
  $file = $_FILES["excel"]["tmp_name"]; // getting temporary source of excel file
  include("Classes/PHPExcel/IOFactory.php"); // Add PHPExcel Library in this code
  $objPHPExcel = PHPExcel_IOFactory::load($file); // create object of PHPExcel library by using load() method and in load method define path of selected file
 
  $output .= "<label class='text-success'>Data Inserted</label><br /><table class='table table-bordered'>";
  foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
  {
   $highestRow = $worksheet->getHighestRow();
   for($row=2; $row<=$highestRow; $row++)
   {
    $output .= "<tr>";
    $test_set = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(0, $row)->getValue());
    $nombre = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
	$estado = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(2, $row)->getValue());
	$tester = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(3, $row)->getValue());
	$fechaejecucion = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(4, $row)->getValue());
    $query = "INSERT INTO informe(	test_set, nombre, estado, tester, fechaejecucion) VALUES ('".$test_set."', '".$nombre."', '".$estado."', '".$tester."', '".$fechaejecucion."')";
    mysqli_query($connect, $query);
    $output .= '<td>'.$test_set.'</td>';
    $output .= '<td>'.$nombre.'</td>';
	$output .= '<td>'.$estado.'</td>';
    $output .= '<td>'.$tester.'</td>';
	$output .= '<td>'.$fechaejecucion.'</td>';
 
    $output .= '</tr>';
   }
  }
  $output .= '</table>';
 
 }
 else
 {
  $output = '<label class="text-danger">Invalid File</label>'; //if non excel file then
 }
}
?>
 
<html>
 <head>
  <title>Import Excel to Mysql using PHPExcel in PHP</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
  <style>
  body
  {
   margin:0;
   padding:0;
   background-color:#f1f1f1;
  }
  .box
  {
   width:700px;
   border:1px solid #ccc;
   background-color:#fff;
   border-radius:5px;
   margin-top:100px;
  }
 
  </style>
 </head>
 <body>
  <div class="container box">
   <h3 align="center">Import Excel to Mysql using PHPExcel in PHP</h3><br />
   <form method="post" enctype="multipart/form-data">
    <label>Select Excel File</label>
    <input type="file" name="excel" />
    <br />
    <input type="submit" name="import" class="btn btn-info" value="Import" />
   </form>
   <br />
   <br />
   <?php
   echo $output;
   ?>
  </div>
 </body>
</html>


Lo que estoy insertando en este codigo es la primera hoja nada mas que tiene las columnas:
test_set
nombre
estado
tester
fechaejecucion

luego tengo 2 hojas mas que tienen por ejemplo:
estado
defecto
proyecto
etc.

Gracias!
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