2 回答
TA贡献1836条经验 获得超4个赞
您需要检测,当它是第一行时,像这样的二进制变量会有所帮助
<?php
require '../vendor/autoload.php';
if (pathinfo($_FILES['upexcel']['name'], PATHINFO_EXTENSION) == 'csv') {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load($_FILES['upexcel']['tmp_name']);
$worksheet = $spreadsheet->getActiveSheet();
$sql = "INSERT INTO `testCommission` (`One`, `two`, `three`, `four`, `five`, `six`, `seven`) VALUES (?, ?, ?, ?, ?, ?, ?)";
$isheader = 0;
foreach ($worksheet->getRowIterator() as $row) {
// Fetch data
if($isheader > 0) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) {
$data[] = $cell->getValue();
}
} else
{ $isheader = 1; }
}
?>
TA贡献1854条经验 获得超8个赞
使用过滤器类。
不要忘记自动加载:
require_once ( 'vendor/autoload.php' );
创建过滤器:
class FirstRowFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter
{
public function readCell($column, $row, $worksheetName = '') {
// Return true for rows after first row
return $row > 0;
}
}
适用于读者:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$filterRow = new FirstRowFilter();
$reader->setReadFilter($filterRow);
加载文件:
$spreadsheet = $reader->load($file);
读取器将仅加载指定行之后的行。
- 2 回答
- 0 关注
- 141 浏览
添加回答
举报