PHPExcel-动态添加行以形成数据库结果

时间:2019-01-31 10:13:42

标签: php phpexcel phpexcelreader

我编写了一个代码,该代码仅生成从数据库返回的一行单元格值。

我希望他们从我的excel文件的第16行开始生成多行。当我想继续生成所有返回的结果(如第16、17、18行等)时,它仅在第16行设置一行选定单元格上..

我该如何解决?

这是我的代码:

     phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filePath);

    $copyFrom= "17";
    $activeSheet = $phpExcelObject->getActiveSheet();

    $rows = $this->get('app')->getData();

    $row = 16;

    if($rows) {

        foreach ($rows as $row) {

            $activeSheet->insertNewRowBefore($copyFrom, 1);
            $activeSheet->duplicateStyle($activeSheet->getStyle('A16'.($copyFrom+1)), 'A16'.$copyFrom);
            $lastColumn = $phpExcelObject->getActiveSheet()->getHighestColumn();
            $rangeFrom = 'A'.($copyFrom+1).':'.$lastColumn.($copyFrom+1);
            $activeSheet->fromArray($activeSheet->rangeToArray($rangeFrom), null, 'A'.$copyFrom);

            $phpExcelObject->setActiveSheetIndex(0)
                ->setCellValue('G7',  $row[0])
                ->setCellValue('H10', $row[1])
                ->setCellValue('G8',  $row[2])
                ->setCellValue('A17', $row[3]);

            $row++;

        }

    }

    $writer = $this->get('phpexcel')->createWriter($phpExcelObject, 'Excel2007');
    $response = $this->get('phpexcel')->createStreamedResponse($writer);
    $dispositionHeader = $response->headers->makeDisposition(
        ResponseHeaderBag::DISPOSITION_ATTACHMENT,
        'the-file.xlsx'
    );
    $response->headers->set('Content-Type', 'text/vnd.ms-excel; charset=utf-8');
    $response->headers->set('Cache-Control', 'maxage=1');
    $response->headers->set('Content-Disposition', $dispositionHeader);

    return $response;
}

我尝试了其他建议,但这对我不起作用。

0 个答案:

没有答案