Question

Just curious about the requirements for the whole PHPExcel project when you want to create a simple dynamically generated Excel file that doesn't exceed more than 125 lines , i'm generating a report of names and stats that go along with these names on my site, although it takes around 15-20 seconds to generate it after you click the button, which is far too long.

Just wondering if you need the whole project to do this or can you(so to speak)trim the fat to make it quicker ?

Any help would be much appreciated

$this->_helper->layout()->disableLayout();
    $data = Factory_Company::getForAdminCompaniesPage(array(
                "pageSize" => 10000,
                "page" => 0
    ));

    $objPHPExcel = new PHPExcel();
    $objPHPExcel->getActiveSheet()->getStyle("A1:J1")->getFont()->setBold(true);
    $objPHPExcel->getActiveSheet()->setAutoFilter('A1:A1');

    $objPHPExcel->getProperties()->setCreator("Stackover")
            ->setLastModifiedBy("john smith")
            ->setTitle("Test Agency List")
            ->setSubject("Test Agency List")
            ->setDescription("Test Agency List")
            ->setKeywords("Test Agency Excel List")
            ->setCategory("Test Agency");



    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Agency')
            ->setCellValue('B1', 'Date Added')
            ->setCellValue('C1', '# of jobs')
            ->setCellValue('D1', '# of candidates')
            ->setCellValue('E1', 'Location')
            ->setCellValue('F1', 'Email')
            ->setCellValue('G1', 'Contact Name')
            ->setCellValue('H1', 'Last Logged in')
            ->setCellValue('I1', 'Sales Rep')
            ->setCellValue('J1', 'Active/Inactive');
    $rowID = 2;
    foreach ($data as $rowArray) {
        $columnID = 'A';
        foreach ($rowArray as $columnValue) {
            $objPHPExcel->getActiveSheet()->setCellValue($columnID . $rowID, $columnValue);
            $columnID++;
        }
        $rowID++;
    }


    $objPHPExcel->getActiveSheet()->setTitle('Intalex Agency List');


    $objPHPExcel->setActiveSheetIndex(0);


    // Redirect output to a client’s web browser (Excel2007)
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Agencies.xlsx"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');

    // If you're serving to IE over SSL, then the following may be needed
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header('Pragma: public'); // HTTP/1.0

    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    $objWriter->save('php://output');
}

}

Was it helpful?

Solution

You can eliminate the nested foreach loop by changing

foreach ($data as $rowArray) {
    $columnID = 'A';
    foreach ($rowArray as $columnValue) {
        $objPHPExcel->getActiveSheet()->setCellValue($columnID . $rowID, $columnValue);
        $columnID++;
    }
    $rowID++;
}

to

foreach ($data as $rowArray) {
   $objPHPExcel->getActiveSheet()->fromArray($rowArray, null, 'A'. $rowID++);
}

or even simply

$objPHPExcel->getActiveSheet()->fromArray($data, null, 'A2');

And eliminate

$objPHPExcel->getActiveSheet()->setAutoFilter('A1:A1');

because it's meaningless setting autofiltering on a single cell, but will add overhead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top