Advertisement
Guest User

Untitled

a guest
Apr 20th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Upload Excel</title>
  6. </head>
  7. <body>
  8. <form action="" method="POST" enctype="multipart/form-data">
  9. <input type="file" name="upload">
  10. <input type="submit" name="submit" value="Simpan">
  11. </form>
  12. <?php
  13. $servername = "localhost";
  14. $username = "root";
  15. $password = "root";
  16. $dbname = "10103010";
  17.  
  18. $conn = new mysqli($servername, $username, $password, $dbname);
  19. if ($conn->connect_error) {
  20. die("Connection failed: " . $conn->connect_error);
  21. }
  22.  
  23. if (isset($_POST['submit'])) {
  24. if (isset($_FILES['upload'])) {
  25. $file_name = $_FILES['upload']['name'];
  26. $file_size = $_FILES['upload']['size'];
  27. $file_tmp = $_FILES['upload']['tmp_name'];
  28. $file_type =$_FILES['upload']['type'];
  29. $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
  30.  
  31. $errors = array();
  32. $extensions = array("xls","xlsx");
  33.  
  34. if (in_array($file_ext,$extensions)===false) {
  35. $errors[]="file tidak didukung, gunakan ekstensi xls atau xlsx";
  36. }
  37.  
  38. if ($file_size > 20097152) {
  39. $errors[]="Ukuranfile harus lebih kecil dari 20 MB";
  40. }
  41.  
  42. if (empty($errors)===true) {
  43. require_once dirname(__FILE__) .'/PHPExcel/Classes/PHPExcel/IOFactory.php';
  44. $objPHPExcel = PHPExcel_IOFactory::load($file_tmp);
  45. $sheet = $objPHPExcel->getSheet();
  46. $highestRow = $sheet->getHighestRow();
  47. $highestColumn = $sheet->getHighestColumn();
  48. echo '<table border="1" width="800" align="center">';
  49. $val=array();
  50. for ($row = 2; $row <= $highestRow; $row++){
  51. $rowData = $sheet->rangeToArray('B' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
  52. foreach($rowData as $cell){
  53. $sql = "INSERT INTO mahasiswa (nim, nama,tempatlahir,tanggallahir,alamat,jurusan) VALUES ('$cell[0]', '$cell[1]', '$cell[2]', '$cell[3]',' $cell[4]', '$cell[5]')";
  54. if ($conn->query($sql) === TRUE) {
  55. echo "New record created successfully<br>";
  56. } else {
  57. echo "Error: " . $sql . "<br>" . $conn->error;
  58. }
  59. echo "<tr>";
  60. foreach ($cell as $key => $value) {
  61. echo '<td>'. $value .'</td>';
  62.  
  63. }
  64. echo "</tr>";
  65. }
  66. }
  67. echo '</table>';
  68. }else{
  69. foreach ($errors as $error) {
  70. echo $error."<br>";
  71. }
  72. }
  73. }
  74. }
  75. $conn->close();
  76. ?>
  77. </body>
  78. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement