我正在尝试使用 PHPEXCEL 将 excel 表插入 mysql DB,我是 php PDO 的初学者,我正在使用以下代码:
<?PHP
if (isset($_FILES["file"]))
{
$info_file_exts = array("csv", "xls", "xlsx");
$info_upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "Excel/csv")
|| ($_FILES["file"]["type"] == "Excel/xls")
|| ($_FILES["file"]["type"] == "Excel/xlsx")
)
&& in_array($info_upload_exts, $info_file_exts))
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$info_file_exts = array("csv", "xls", "xlsx");
if($info_file_exts[0]=='csv'){
$ink=explode('.'.$info_file_exts[0],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[1]=='xls')
{
$ink=explode('.'.$info_file_exts[1],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
else if($info_file_exts[2]=='xlsx')
{
$ink=explode('.'.$info_file_exts[2],$_FILES["file"]["name"]);
$time=time();
$info_new_file_name =$ink[0].'_'.$time.'.'.$info_file_exts[0];
}
}
$path = ($_FILES["file"]["tmp_name"]);
$objPHPExcel = PHPExcel_IOFactory::load($path);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
}
$nrColumns = ord($highestColumn) - 64;
echo "File ".$worksheetTitle." has ";
echo $nrColumns . ' columns';
echo ' y ' . $highestRow . ' rows.';
echo 'Data: <table width="100%" cellpadding="3" cellspacing="0"><tr>';
for ($row = 1; $row <= $highestRow; ++ $row)
{
echo '<tr>';
for ($col = 0; $col < $highestColumnIndex; ++ $col)
{
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val = $cell->getValue();
if($row === 1)
echo '<td style="background:#000; color:#fff;">' . $val . '</td>';
else
echo '<td>' . $val . '</td>';
}
echo '</tr>';
}
echo '</table>';
for ($row = 2; $row <= $highestRow; ++$row)
{
$val=array();
for ($col = 0; $col < $highestColumnIndex; ++ $col)
{
$cell = $worksheet->getCellByColumnAndRow($col, $row);
$val[] = $cell->getValue();
}
$email=$val[0];
$pass= $val[1];
$sql="insert into users (email, password) VALUES (:email,:pass)";
$q = $conn->prepare($sql);
$q->execute(array(':email'=>$email,':pass'=>$pass));
}
?>
<div style="width: 500px; margin: 200px auto 0 auto;">
<form action="display.php" method="post" enctype="multipart/form-data" >
<label for="file">Filename: </label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit" style="margin-left:90PX">
</form>
</div>
这段代码工作正常,但我想做的是计算添加了多少行?,我尝试使用rowCount()
函数,但它返回每个插入的输出,我想将它与所有插入任务一起使用,如果用户多次单击提交按钮,则会显示错误,我该如何解决?