gpt4 book ai didi

phpoffice/phpspreadsheet - 如何从 getHighestRow() 中排除没有值的单元格

转载 作者:可可西里 更新时间:2023-10-31 23:44:45 24 4
gpt4 key购买 nike

$eanStyle = new \PHPExcel_Style();
$eanStyle->getNumberFormat()->applyFromArray([
'code' => '0000000000000'
]);

/* apply styles */
$mainSheet->duplicateStyle($eanStyle, 'A2:A10000');

上面的代码生成.xlsx模板文件,用户输入数据(7行)并上传文件,然后:

$mainSheet->getHighestRow('A'); //  retruns 10000 instead of 8 (7 rows + header)

在此先感谢您的帮助。

最佳答案

我建议您为 read only specific rows and columns 创建一个读取过滤器.这将防止包含其他空行:

$inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
$sheetname = 'Data Sheet #3';

/** Define a Read Filter class implementing \PhpOffice\PhpSpreadsheet\Reader\IReadFilter */
class MyReadFilter implements \PhpOffice\PhpSpreadsheet\Reader\IReadFilter {
public function readCell($column, $row, $worksheetName = '') {
// Read rows 1 to 7 and columns A to E only
if ($row >= 1 && $row <= 7) {
if (in_array($column,range('A','E'))) {
return true;
}
}
return false;
}
}

/** Create an Instance of our Read Filter **/
$filterSubset = new MyReadFilter();

/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Tell the Reader that we want to use the Read Filter **/
$reader->setReadFilter($filterSubset);
/** Load only the rows and columns that match our filter to Spreadsheet **/
$spreadsheet = $reader->load($inputFileName);

关于phpoffice/phpspreadsheet - 如何从 getHighestRow() 中排除没有值的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52871963/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com