Home:ALL Converter>excel file upload using phpexcel and data insertion to the mysql database

excel file upload using phpexcel and data insertion to the mysql database

Ask Time:2017-03-08T13:42:27         Author:Chamath Gunasekara

Json Formatter

kindly help me to sort this out i just need to upload an excel file to upload folder same time export data to the mysql database.

currently uploading is successfully happen and if i give excel file location and file name manually data within it will export to the database. kindly tell me what method should it used to do this same time.

code set used to upload excel file to the 'uploads' folder

<?php
require_once './config/MainConfig.php';
include './config/dbc.php';


$uploadedStatus = 0;

if (isset($_POST["submit"])) {
    if (isset($_FILES["file"])) {
//        $_SESSION['date_ss'] = $_POST['date_ss'];
//if there was an error uploading the file
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        } else {
            if (file_exists($_FILES["file"]["name"])) {
                unlink($_FILES["file"]["name"]);
                $uploadedStatus = 2;
            }
            $name = basename($_FILES['file']['name']);
            $name1 = explode('.', $name);
            if ($name1[count($name1) - 1] == 'csv' || $name1[count($name1) - 1] == 'xlsx') {

                $target_path = "uploads/";
                $target_location = $target_path . basename($_FILES['file']['name']);
                $_SESSION['target_location'] = $target_location;


//            $datess = $_POST['date_ss'];
                move_uploaded_file($_FILES["file"]["tmp_name"], $target_location);
                $uploadedStatus = 1;
            }
        }
    } else {
        echo "No file selected <br />";
    }
}
?>

<html>
    <head>
        <style>
            .file-upload {
                max-width: 580px;
                height: 200px;
                padding: 25px 35px 45px;
                margin: 0 auto;
                background-color: #fff;
                border: 1px solid rgba(0,0,0,0.1);  
            }

        </style>
    </head>

    <?php 
    if (array_key_exists("action", $_POST)) {
          if ($_POST['action'] == 'sendManualFileUpoadingData') {
              $Manual_note_No=$_POST['Manual_note_No'];
              $Phone_amount= $_POST[' Phone_amount'];
              echo $Manual_note_No;

          }
    }

    ?>
    <div class="container">
        <div class="wrapper">
            <div class="file-upload">  
                <div class="row">
                    <div class="col-md-4">Transfer Note Number :</div>
                    <div class="col-md-4"><?php echo''  ?></div>
                </div>
                <div class="row">
                    <div class="col-md-4">Phone quantity:</div>
                    <div class="col-md-4"><?php echo '' ?></div>
                </div>
                <div class="row">
                <div class="col-md-4"></div>
                <div class="col-md-4">
                    <form action="fileuploadexecution.php" method="post" enctype="multipart/form-data">
                        <input type="file" id="file" name="file" multiple="multiple" />
                        <p style="text-align: right; margin-top: 20px;">
                            <input type="submit"  value="Upload Files" name="submit" class= "btn btn-success" />
                        </p>
                    </form>
                </div>
                <div class="col-md-4"></div>
                </div>
                <div class="row">
                    <?php
                    if ($uploadedStatus == 1) {
                        echo 'file uploaded successfully';
                    } elseif ($uploadedStatus == 2) {
                        echo 'file already available';
                    }
                    ?>
                </div>

            </div>
        </div>    
    </div>



    <!-- you need to include the ShieldUI CSS and JS assets in order for the Upload widget to work -->
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    <script type="text/javascript" src="js/jquery.min.js"></script>
</html>

code set used to export excel file data into mysql table

<?php

session_start();
//all save,update,delete

require_once './config/dbc.php';
//db connectin
require_once './class/database.php';
require_once './class/systemSetting.php';
$system = new setting();
//calling the class setting from systemsetting.php
$database = new database();

//                                MainConfig::connectDB();
//                                $datess = $_SESSION['date_ss'];
//                                $q = mysql_fetch_array(mysql_query("SELECT MAX(commission.num_of_session +1)AS commax FROM commission"));
//                                $sess = $q['commax'];


set_include_path(get_include_path() . PATH_SEPARATOR . 'ex_class/');
include './xl_upload/ex_class/PHPExcel/IOFactory.php';


// This is the file path to be uploaded.
//
//echo $_SESSION['target_location'];
//$inputFileName = $target_path . basename($_FILES['file']['name']);

 $inputFileName = 'testFile.xlsx';

try {
    $objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch (Exception $e) {
    die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage());
}


$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
$count = 1;
for ($count; $count <= $arrayCount; $count++) {
    $Doc_No = trim($allDataInSheet[$count]["A"]);
    $ESN = trim($allDataInSheet[$count]["B"]);

    $insertTable = mysql_query("INSERT INTO `test_table` (`Doc_No`, `ESN`) VALUES ('".$Doc_No."','".$ESN."');") or die(mysql_error());
}
$msg = 'Record has been added. <div style="Padding:20px 0 0 0;"><a href="commision_data_upolad.php">Go Back</a></div>';

?>

excel file that going to upload

Author:Chamath Gunasekara,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/42663824/excel-file-upload-using-phpexcel-and-data-insertion-to-the-mysql-database
yy