如何从 EXCEL 中获取 PHP 中的分组行?

How get grouping rows in PHP from EXCEL?

如何从 EXCEL 中获取 PHP 中的分组行?

此代码不发群:

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("bulat_price.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->toArray();
$spreadsheet = PHPExcel_IOFactory::load($file); // load file
$objWorksheet = $spreadsheet->getActiveSheet();
$worksheet = $spreadsheet->setActiveSheetIndex(0); // select firts sheet

$i = 0;
$arrLevel = [];

foreach ($worksheet->getRowDimensions() as $rowDimension) {
    $i++;
    $arrLevel[$i]['level'] = $rowDimension->getOutlineLevel(); // get level
}
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'

foreach ($objWorksheet->getRowIterator() as $row) {    
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(true);

    foreach ($cellIterator as $cell) {
        $arrLevel[$row->getRowIndex()]["excel"][$cell->getColumn()] = $cell->getValue(); // merge level and value
    }
}

var_dump($arrLevel);

我们得到:

enter image description here

您可以通过为每一行设置它来设置一系列行(或列)的大纲组;您可以通过设置组级别参数来嵌套大纲组。

// Set outline levels
for ($row = 2; $row <= 10; ++$row) {
    $objPHPExcel->getActiveSheet()
        ->getRowDimension($row)
            ->setOutlineLevel(1)
            ->setVisible(false)
            ->setCollapsed(true);
}

for ($row = 4; $row <= 9; ++$row) {
    $objPHPExcel->getActiveSheet()
        ->getRowDimension($row)
            ->setOutlineLevel(2)
            ->setVisible(false)
            ->setCollapsed(true);
}
for ($row = 6; $row <= 8; ++$row) {
    $objPHPExcel->getActiveSheet()
        ->getRowDimension($row)
            ->setOutlineLevel(3)
            ->setVisible(false)
            ->setCollapsed(true);
}