Advertisement
CPV

load xlsx and insert in database

CPV
Oct 6th, 2017
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2. echo '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>';
  3. echo '<h2>CARGAR EXCEL</h2>
  4. <form method="post" enctype="multipart/form-data">
  5. *.XLSX <input type="file" name="file"  />&nbsp;&nbsp;<input type="submit" value="Procesar..." />
  6. </form>';
  7. echo "<h3>FAVOR DE RESPETAR EL FORMATO DE EXCEL DE 3 COLUMNAS : MATRÍCULA - NOMBRES , CONDICIÓN</h3>";
  8. if (isset($_FILES['file'])) {
  9.     $host = "xxxxxxxxxxxx";
  10.     $user = "xxxxxxt";
  11.     $password = 'xxxxxxxxxxxxxxxxxxxxxxx';
  12.     $database = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  13.     $link = mysqli_connect($host, $user, $password, $database);
  14.     mysqli_query($link, "truncate estadodecolegiados;");
  15.     require_once __DIR__ . '/simplexlsx.class.php';
  16.     if ($xlsx = SimpleXLSX::parse($_FILES['file']['tmp_name'])) {
  17.         $array = $xlsx->rows();
  18.         foreach ($array as $key => $value) {
  19.                 $sql = "insert into estadodecolegiados(matricula,nombre,condicion) values('".$value[0]."','". utf8_decode($value[1])."','".$value[2]."');";
  20.                 mysqli_query($link, $sql);
  21.         }
  22.                 echo '<h2>RESULTADOS</h2>';
  23.         echo '<table border="1" cellpadding="3" style="border-collapse: collapse">';
  24.         list( $cols, ) = $xlsx->dimension();
  25.         foreach ( $xlsx->rows() as $k => $r ) {
  26.             //      if ($k == 0) continue; // skip first row
  27.             echo '<tr>';
  28.             for ( $i = 0; $i < $cols; $i ++ ) {
  29.                 echo '<td>' . ( ( isset( $r[ $i ] ) ) ? $r[ $i ] : '&nbsp;' ) . '</td>';
  30.             }
  31.             echo '</tr>';
  32.         }
  33.         echo '</table>';
  34.                
  35.                
  36.                      
  37.     }else {
  38.          echo SimpleXLSX::parse_error();
  39.     }
  40.    
  41.         mysqli_close($link);
  42. }
  43.  
  44. echo '</body></html>';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement