PHPExcel-用数据库中的数据插入新行

时间:2019-01-24 12:01:50

标签: php phpexcel

使用“ getData”功能,我仅将一行数据导出到我的excel文件中。我试图为要返回的每组对象动态插入尽可能多的行,而不仅仅是返回一行。

我尝试过的是:

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

    $copyFrom= "17";
    $activeSheet = $phpExcelObject->getActiveSheet();
    $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);

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

    if ($rows){
        $phpExcelObject->getActiveSheet()->setCellValueByColumnAndRow(0, $lastColumn . 2);
    }

    if($rows) {
        foreach ($rows as $row) {
            $phpExcelObject->setActiveSheetIndex(0)
                ->setCellValue('A17', $row[3])
                ->setCellValue('E17', 'kom.')
                ->setCellValue('F17', '5')
                ->setCellValue('G17', $row[4])
                ->setCellValue('H17', $row[4]);
        }
    }

    $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 个答案:

没有答案