2016-11-20 175 views
0

我的代碼無法將Excel文件中的數據導入到phpMySql中。我花了很多時間來找到錯誤,但我不能,所以請,任何人都可以幫助我通過這個不好的時間無法使用PHPExcel將Excel中的數據導入到phpMySql中

我運行此代碼後,顯示我沒有錯誤,但當我訪問我的phpMyAdmin,沒有任何數據來自Excel。

使用example.php

<?php 

include ("PHPExcel/IOFactory.php"); 

$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "dashboard_db"; 
$tbname_act = "average_cycle_time"; 

$inputFileType = 'Excel2007'; 
$inputFileName = 'example.xlsx'; 
$sheetnames = 'ACT' ; 

$connect = mysqli_connect($servername, $username, $password, $dbname); 
if ($connect -> connect_error){ 
die ("connection failed: " . $conn -> connect_error); 
} 
echo "Connection successful ! <br>" ; 

$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
$objPHPExcel = $objReader->load($inputFileName); 
$objReader->setLoadSheetsOnly($sheetnames); 


$html="<table border='1'>"; 
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName); 
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
{ 
    $highestRow = $worksheet->getHighestRow(); 
    for ($row=2; $row<=$highestRow; $row++) 
    {   
     $html.="<tr>"; 

     $TruckID = mysqli_real_escape_string($connect, $worksheet- >getCellByColumnAndRow(0, $row)->getValue()); 
     $Date = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue()); 
     $ArrivalTime = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(2, $row)->getValue()); 
     $DepartTime = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(3, $row)->getValue()); 

     $sql = "INSERT INTO $tbname_act(excel_TruckID, excel_Date, excel_ArrivalTime, excel_DepartTime) 
       VALUES ('".$TruckID."', '".$Date."', '".$ArrivalTime."', '".$DepartTime."')"; 

     mysqli_query($connect, $sql); 

     $html.= '<td>'.$TruckID.'</td>'; 
     $html .= '<td>'.$Date.'</td>'; 
     $html .= '<td>'.$ArrivalTime.'</td>'; 
     $html .= '<td>'.$DepartTime.'</td>'; 
     $html .= "</tr>"; 
    } 
    } 
    $html .= '</table>'; 
    echo $html; 
    echo '<br />Time for calculation of Average Cycle Time ... Inserted !!'; 
    ?> 

create.sql

CREATE TABLE IF NOT EXISTS `average_cycle_time` ( 
`TruckID` int(10) NOT NULL, 
`Date` date NOT NULL, 
`ArrivalTime` timestamp NOT NULL, 
`DepartTime` timestamp NOT NULL, 
PRIMARY KEY (`TruckID`) 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ; 

Data in Excel file(.xslx)

+0

也許看看日期值:它是MS Excel序列化日期時間標記還是格式化字符串? –

回答

0

它可能是你的SQL查詢是不正確的。您可以通過顯示$ sql的值來調試sql查詢,然後從Phpmyadmin運行sql查詢

相關問題