我正在努力找到一个教程,告诉我如何编写一个php脚本,它可以让我上传并将xls(不是CSV)插入到mysql中。我有这个PHP脚本fgetcsv,它的工作都很好。但我需要包含对xls的支持。上传并插入xls文件到MySQL?
我的PHP
if(isset($_POST["Import"])){
echo $filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
//It wiil insert a row to our subject table from our csv file`
$sql = "INSERT into excell_import (`objekt`, `element_nummer`, `element_hojd`, `element_langd`,COURSE_ID, `AY`, `SEMESTER`)
values('$emapData[1]','$emapData[2]','$emapData[3]','$emapData[4]','$emapData[5]','$emapData[6]','$em apData[7]')";
//we are using mysql_query function. it returns a resource on true else False on error
$result = mysql_query($sql, $conn);
if(! $result)
{
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"index.php\"
</script>";
}
}
fclose($file);
//throws a message if data successfully imported to mysql database from excel file
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"index.php\"
</script>";
//close of connection
mysql_close($conn);
}
?>
尝试谷歌的PHP的excel库,'PHPExcel'例如 – 2014-09-26 11:22:35
所以其中许多PHP库,其能够读取真实如[PHPExcel](https://phpexcel.codeplex.com/)的您实际上看过BIFF格式的xls文件吗? – 2014-09-26 11:22:56
尝试谷歌您的需求,有一些图书馆已经存在这个,如https://phpacademy.org/topics/phpexcel-inserting-xlsxxls-to-mysql/31882 – 2014-09-26 11:23:25