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

时间:2018-05-04 20:58:08

标签: php excel phpexcel phpspreadsheet phpoffice

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

enter image description here

此代码不会发布组:

$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load("bulat_price.xlsx");
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->toArray();

2 个答案:

答案 0 :(得分:0)

$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

答案 1 :(得分:0)

您可以通过为每行设置轮廓组来设置行(或列)范围内的轮廓组。您可以通过设置组级别参数来嵌套大纲组。

// 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);
}