Guest User

Untitled

a guest
May 28th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2. //Incluyo la clase
  3. include 'simplexlsx.class.php';
  4. $xlsx = new SimpleXLSX( 'countries_and_population.xlsx' );//Instancio la clase y le paso como parametro el archivo a leer
  5. $fp = fopen( 'datos.csv', 'w');//Abrire un archivo "datos.csv", sino existe se creara
  6. foreach( $xlsx->rows() as $fields ) {//Itero la hoja de calculo
  7. fputcsv( $fp, $fields);//Doy formato CSV a una línea y le escribo los datos
  8. }
  9. fclose($fp);//Cierro el archivo "datos.csv"
  10. ?>
  11.  
  12. <?php
  13. $db_host="localhost";
  14. $db_name="ciudades";
  15. $db_user="root";
  16. $db_pass="";
  17. include 'simplexlsx.class.php';
  18. $xlsx = new SimpleXLSX( 'countries_and_population.xlsx' );
  19. try {
  20. $conn = new PDO( "mysql:host=$db_host;dbname=$db_name", "$db_user", "$db_pass");
  21. $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  22. }
  23. catch(PDOException $e)
  24. {
  25. echo $sql . "<br>" . $e->getMessage();
  26. }
  27. $stmt = $conn->prepare( "INSERT INTO countries_and_population (estado, ciudad, poblacion, dato_estimado, sociedad) VALUES (?, ?, ?, ?, ?)");
  28. $stmt->bindParam( 1, $estado);
  29. $stmt->bindParam( 2, $ciudad);
  30. $stmt->bindParam( 3, $poblacion);
  31. $stmt->bindParam( 4, $dato_estimado);
  32. $stmt->bindParam( 5, $sociedad);
  33. foreach ($xlsx->rows() as $fields)
  34. {
  35. $estado = $fields[0];
  36. $ciudad = $fields[1];
  37. $poblacion = $fields[2];
  38. $dato_estimado = $fields[3];
  39. $sociedad = $fields[4];
  40. $stmt->execute();
  41. }
  42.  
  43. ?>
Add Comment
Please, Sign In to add comment