繁体   English   中英

使用phpexcel上传excel文件,并将数据插入mysql数据库

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

请帮我解决这个问题,我只需要上传一个excel文件来上传文件夹,同时将数据导出到mysql数据库。

当前成功上传成功,如果我手动指定excel文件的位置和文件名,其中的数据将导出到数据库。 请告诉我同时应使用哪种方法。

用于将excel文件上传到“ uploads”文件夹的代码集

<?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>

用于将Excel文件数据导出到mysql表中的代码集

<?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文件

试试这个

从PHP文件执行数据库备份查询。 以下是使用SELECT INTO OUTFILE查询创建表备份的示例:

$tableName  = 'yourtable';
$backupFile = 'backup/yourtable.sql';
$query      = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysqli_query($con,$query);

要还原备份,您只需要运行LOAD DATA INFILE查询,如下所示:

$tableName  = 'yourtable';
$backupFile = 'yourtable.sql';
$query      = "LOAD DATA INFILE 'backupFile' INTO TABLE $tableName";
$result = mysqli_query($con,$query);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM