PHP 导入excel

时间:2023-03-08 17:10:25
PHP 导入excel

db.php为数据库操作类, $config为数据库配置,PHPExcel版本为PHPExcel_1.8.0,  PHP代码:

header("Content-type:text/html;charset=utf-8");
$dir = dirname(__FILE__);
require $dir . "/PHPExcel/db.php";
require $dir . "/PHPExcel/PHPExcel/IOFactory.php";
$fileName = $dir . "/export.xlsx"; //全部加载
//$object = PHPExcel_IOFactory::load($fileName); //部分加载
$fileType = PHPExcel_IOFactory::identify($fileName);
$objReader = PHPExcel_IOFactory::createReader($fileType);
$sheetName = "一年级";
$objReader->setLoadSheetsOnly($sheetName);
$object = $objReader->load($fileName); //循环输出
foreach($object->getWorksheetIterator() as $sheet) {
foreach($sheet->getRowIterator() as $row) {
if($row->getRowIndex() < 2) {
continue;
}
foreach($row->getCellIterator() as $cell) {
$data = $cell->getValue();
echo $data." ";
}
echo "<br>";
}
echo "<br>";
}

Excel :

PHP 导入excel

效果图:

PHP 导入excel