获取 XLSX 文件的行数
Get the row count of an XLSX file
我想创建一个包含我计算机上所有 .xlsx
个文件的列表以及其中的行数。
获取每个 .xlsx
文件中行数的最佳方法是什么?我在 Mac OSX Yosemite(如果相关)。答案可以是任何语言。
您可以使用 PHP 和 Spout (https://github.com/box/spout) 来完成。
代码看起来像这样:
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;
// This is the root directory you want to list the XLSX files of
// CHANGE IT !!
$rootPath = '/path/to/root/directory';
// this will read the XLSX file at the given path and return the number of rows for the file
function getNumRows($xlsxPath) {
$numRows = 0;
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($xlsxPath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$numRows++;
}
}
$reader->close();
return $numRows;
}
// This will recursively go through the root folder and all its subfolders
$directoryIterator = new \RecursiveDirectoryIterator($rootPath, RecursiveDirectoryIterator::SKIP_DOTS);
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $fileInfo) {
if (strtolower($fileInfo->getExtension()) === 'xlsx') {
$xlsxPath = $fileInfo->getPathname();
$numRows = getNumRows($xlsxPath);
// store the number of rows for the file somewhere
}
}
我想创建一个包含我计算机上所有 .xlsx
个文件的列表以及其中的行数。
获取每个 .xlsx
文件中行数的最佳方法是什么?我在 Mac OSX Yosemite(如果相关)。答案可以是任何语言。
您可以使用 PHP 和 Spout (https://github.com/box/spout) 来完成。 代码看起来像这样:
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Common\Type;
// This is the root directory you want to list the XLSX files of
// CHANGE IT !!
$rootPath = '/path/to/root/directory';
// this will read the XLSX file at the given path and return the number of rows for the file
function getNumRows($xlsxPath) {
$numRows = 0;
$reader = ReaderFactory::create(Type::XLSX);
$reader->open($xlsxPath);
foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$numRows++;
}
}
$reader->close();
return $numRows;
}
// This will recursively go through the root folder and all its subfolders
$directoryIterator = new \RecursiveDirectoryIterator($rootPath, RecursiveDirectoryIterator::SKIP_DOTS);
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);
foreach ($recursiveIterator as $fileInfo) {
if (strtolower($fileInfo->getExtension()) === 'xlsx') {
$xlsxPath = $fileInfo->getPathname();
$numRows = getNumRows($xlsxPath);
// store the number of rows for the file somewhere
}
}