Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Upload Excel</title>
- </head>
- <body>
- <form action="" method="POST" enctype="multipart/form-data">
- <input type="file" name="upload">
- <input type="submit" name="submit" value="Simpan">
- </form>
- <?php
- $servername = "localhost";
- $username = "root";
- $password = "root";
- $dbname = "10103010";
- $conn = new mysqli($servername, $username, $password, $dbname);
- if ($conn->connect_error) {
- die("Connection failed: " . $conn->connect_error);
- }
- if (isset($_POST['submit'])) {
- if (isset($_FILES['upload'])) {
- $file_name = $_FILES['upload']['name'];
- $file_size = $_FILES['upload']['size'];
- $file_tmp = $_FILES['upload']['tmp_name'];
- $file_type =$_FILES['upload']['type'];
- $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
- $errors = array();
- $extensions = array("xls","xlsx");
- if (in_array($file_ext,$extensions)===false) {
- $errors[]="file tidak didukung, gunakan ekstensi xls atau xlsx";
- }
- if ($file_size > 20097152) {
- $errors[]="Ukuranfile harus lebih kecil dari 20 MB";
- }
- if (empty($errors)===true) {
- require_once dirname(__FILE__) .'/PHPExcel/Classes/PHPExcel/IOFactory.php';
- $objPHPExcel = PHPExcel_IOFactory::load($file_tmp);
- $sheet = $objPHPExcel->getSheet();
- $highestRow = $sheet->getHighestRow();
- $highestColumn = $sheet->getHighestColumn();
- echo '<table border="1" width="800" align="center">';
- $val=array();
- for ($row = 2; $row <= $highestRow; $row++){
- $rowData = $sheet->rangeToArray('B' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
- foreach($rowData as $cell){
- $sql = "INSERT INTO mahasiswa (nim, nama,tempatlahir,tanggallahir,alamat,jurusan) VALUES ('$cell[0]', '$cell[1]', '$cell[2]', '$cell[3]',' $cell[4]', '$cell[5]')";
- if ($conn->query($sql) === TRUE) {
- echo "New record created successfully<br>";
- } else {
- echo "Error: " . $sql . "<br>" . $conn->error;
- }
- echo "<tr>";
- foreach ($cell as $key => $value) {
- echo '<td>'. $value .'</td>';
- }
- echo "</tr>";
- }
- }
- echo '</table>';
- }else{
- foreach ($errors as $error) {
- echo $error."<br>";
- }
- }
- }
- }
- $conn->close();
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement