我有一個代碼,我可以在我的mysql中導入.CSV文件。但我無法導入Excel文件。當我導入Excel文件時,一些垃圾值插入到我的數據庫中。如何導入mysql中的Excel文件?
<?php
if(isset($_POST["Import"]))
{
//First we need to make a connection with the database
include("connection.php");
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
//$sql_data = "SELECT * FROM prod_list_1 ";
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//print_r($emapData);
//exit();
$sql = "INSERT into city(city_id,city_name) values ('$emapData[0]','$emapData[1]')";
mysql_query($sql);
}
fclose($file);
echo 'CSV File has been successfully Imported';
header('Location: index.php');
}
else
echo 'Invalid File:Please Upload CSV File';
}
?>
<form enctype="multipart/form-data" method="post" role="form">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="Import" value="Import">Upload</button>
</form>
你有沒有仔細看看你的Excel CSV文件?有時在Excel中有多行單元會造成麻煩。 – cars10m
你可以顯示「垃圾值」的樣本嗎? – Jens
在我的excel文件中只有2個列像City Id,City Name。垃圾值就像ÐÏࡱá,ÂCalib,õÿàôàà –