Exceldate更改为不同的date在MySQL中
我想导入Excel文件到MySQL数据库。 我有excel中的date和IMEI字段导入数据库。 但是在导入到数据库的时候,我的date变成了不同的date,IMEI号也变成了指数格式。 我该如何解决这个问题。
这是图片。
这里是代码。
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "portal"; //$exceldata = array(); // Create connection $con = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$con) { die("Connection failed: " . mysqli_connect_error()); } /** Set default timezone (will throw a notice otherwise) */ date_default_timezone_set('Asia/Kolkata'); include 'Classes2/PHPExcel/IOFactory.php'; if(isset($_FILES['file']['name'])){ // $file_name = $_FILES['file']['name']; // $ext = pathinfo($file_name, PATHINFO_EXTENSION); //Checking the file extension $file_name = $_FILES['file']['tmp_name']; $inputFileName = $file_name; if(is_uploaded_file($inputFileName)){ // Read your Excel workbook try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName); $objReader = PHPExcel_IOFactory::createReader($inputFileType); $objPHPExcel = $objReader->load($inputFileName); } catch (Exception $e) { die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . $e->getMessage()); } //Table used to display the contents of the file echo '<center><table style="width:50%;" border=1>'; // Get worksheet dimensions $sheet = $objPHPExcel->getSheet(0); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); // Loop through each row of the worksheet in turn for ($row = 2; $row <= $highestRow; $row++) { // Read a row of data into an array $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE); //check whether member already exists in database with same email $prevQuery = "SELECT id FROM activereport WHERE aIMEI = '".$rowData[0][1]."' "; // $prevResult = $con->query($prevQuery); $prevResult = mysqli_query($con,$prevQuery); $count = mysqli_num_rows($prevResult); if($count > 0){ // //update member data $sql = " UPDATE activereport SET modelno= '".$rowData[0][0]."', dateOfActivation='" . $rowData[0][2] . "' WHERE aIMEI = '" .$rowData[0][1]."' "; } else{ $sql = " INSERT INTO activereport (modelno, aIMEI, dateOfActivation) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$rowData[0][2]."')"; } if (mysqli_query($con, $sql)) { $exceldata[] = $rowData[0]; } else { echo "Error: " . $sql . "<br>" . mysqli_error($con); } } echo '</table></center>'; //redirect to the listing page header("Location: Import_Active_Data.php"); } else{ echo '<p style="color:red;">Please upload valid</p>'; } } ?>
这种行为的一个原因可能是Excel存储date的方式:通过计算自1900年1月1日以来的日子。如果将它作为parameter passing给SQL查询,它可能会发生奇怪的事情。
尝试将Exceldate转换为一个string,并将其传递给SQL查询。
所以,我会改变这个块:
if($count > 0){ // //update member data $sql = " UPDATE activereport SET modelno= '".$rowData[0][0]."', dateOfActivation='" . $rowData[0][2] . "' WHERE aIMEI = '" .$rowData[0][1]."' "; } else{ $sql = " INSERT INTO activereport (modelno, aIMEI, dateOfActivation) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$rowData[0][2]."')"; }
进入这个:
if($count > 0){ // Date-to-string conversion start $exceldate = $rowData[0][2]; $phpdate = new DateTime(); $phpdate->setDate(1899, 12, 30); $dateint = new DateInterval("P" . $exceldate . "D"); $phpdate->add($dateint); $datestring = $phpdate->format("Ymd"); // Date-to-string conversion end // //update member data $sql = " UPDATE activereport SET modelno= '".$rowData[0][0]."', dateOfActivation='" . $datestring . "' WHERE aIMEI = '" .$rowData[0][1]."' "; } else{ $sql = " INSERT INTO activereport (modelno, aIMEI, dateOfActivation) VALUES ('".$rowData[0][0]."','".$rowData[0][1]."','".$datestring."')"; }
直接使用rowData [0] [2],将把Excel单元格的值(2017-11-11 = 43050)转换成一个phpstring('2017-11-11'),传递给sql查询。