使用PHP从特定行/列将Excel数据导入MySQL

时间:2014-06-05 22:36:52

标签: php mysql excel

我已经调查了一下如何导入excel到mysql qith php,它很好,但我的问题是excel有一些列名和不必要的东西,我要开始获取信息的行是第3行,列,它也是第3列,所以,我怎么能这样做,我在PHP上做,即时发布我用作基础的代码来启动我的项目。

<?php
if(isset($_POST["Import"]))
{
$host="localhost"; // Host name.
$db_user="root";
$db_password="";
$db='myDB'; // Database name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());

echo $filename=$_FILES["file"]["tmp_name"];
//echo $ext=substr($filename,strrpos($filename,"."),(strlen($filename)-strrpos($filename,".")));


 if($_FILES["file"]["size"] > 0)
 {

  $file = fopen($filename, "r");
         while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
         {
            print_r($emapData);
            $sql = "INSERT into myTable(name,address,email,password) values('$emapData[0]','$emapData[1]','$emapData[2]','$emapData[3]')";
            mysql_query($sql);
         }
         fclose($file);
         echo "CSV File has been successfully Imported";
 }
 else
 echo "Invalid File:Please Upload CSV File";

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import CSV/Excel file</title>
</head>
<body>
<form enctype="multipart/form-data" method="post">
<table border="1" width="40%" align="center">
<tr >
<td colspan="2" align="center"><strong>Import CSV/Excel file</strong></td>
</tr>

<tr>
<td align="center">CSV/Excel File:</td><td><input type="file" name="file" id="file"></td></tr>
<tr >
<td colspan="2" align="center"><input type="submit" name="Import" value="Import"></td>
</tr>
</table>
</form>
</body>
</html>

它的代码很好,我在几乎每个问题中看到的教程,我只想知道如何从第3行和第3列中重新选择,因为其他行/列具有无法删除的不必要的信息, (对于我正在做的应用)。

0 个答案:

没有答案