跳过 PHPSpreadsheet 导入中的第一行
Skip First Row in PHPSpreadsheet Import
刚开始使用 phpspreadsheet。我想弄清楚如何在上传时跳过 excel 文件中的 header 行。
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 (?, ?, ?, ?, ?, ?, ?)";
foreach ($worksheet->getRowIterator() as $row) {
// Fetch data
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) {
$data[] = $cell->getValue();
}
如何使用上述脚本实现此目的?
你需要检测,当它是第一行时,这样的二进制变量有帮助
<?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; }
}
?>
使用过滤器 class。
请参阅文档:
https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-files/#reading-only-specific-columns-and-rows-from-a-file-read-filters
不要忘记自动加载:
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:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$filterRow = new FirstRowFilter();
$reader->setReadFilter($filterRow);
加载文件:
$spreadsheet = $reader->load($file);
reader 只会加载指定行之后的行。
刚开始使用 phpspreadsheet。我想弄清楚如何在上传时跳过 excel 文件中的 header 行。
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 (?, ?, ?, ?, ?, ?, ?)";
foreach ($worksheet->getRowIterator() as $row) {
// Fetch data
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
$data = [];
foreach ($cellIterator as $cell) {
$data[] = $cell->getValue();
}
如何使用上述脚本实现此目的?
你需要检测,当它是第一行时,这样的二进制变量有帮助
<?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; }
}
?>
使用过滤器 class。 请参阅文档: https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-files/#reading-only-specific-columns-and-rows-from-a-file-read-filters
不要忘记自动加载:
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:
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");
$filterRow = new FirstRowFilter();
$reader->setReadFilter($filterRow);
加载文件:
$spreadsheet = $reader->load($file);
reader 只会加载指定行之后的行。