我找到了这个代码并且它工作得很好,除了它将所有工作表导入我的MySql数据库并且我只打算使用第一个工作表。
<?php
$connect = mysqli_connect("localhost", "root", "", "test_db");
include ("PHPExcel/IOFactory.php");
$html="<table border='1'>";
$objPHPExcel = PHPExcel_IOFactory::load('example.xls');
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$highestRow = $worksheet->getHighestRow();
for ($row=2; $row<=$highestRow; $row++)
{
$html.="<tr>";
$name = mysqli_real_escape_string($connect, $worksheet- >getCellByColumnAndRow(0, $row)->getValue());
$email = mysqli_real_escape_string($connect, $worksheet->getCellByColumnAndRow(1, $row)->getValue());
$sql = "INSERT INTO tbl_excel(excel_name, excel_email) VALUES ('".$name."', '".$email."')";
mysqli_query($connect, $sql);
$html.= '<td>'.$name.'</td>';
$html .= '<td>'.$email.'</td>';
$html .= "</tr>";
}
}
$html .= '</table>';
echo $html;
echo '<br />Data Inserted';
?>
我试过了 $ objWorksheet = $ objPHPExcel-&gt; setActiveSheetIndex(0); 以及getSheet(0); 还有很多其他事情无济于事。