Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PhpSpreadsheet with large data

i have a multidimensional array with 3070 Values

$tbl= array(
  array(
    "KDNR" => 1,
    "GESCHL" => "test",
    "TITEL" => "test",
    "VORNAME" => "test",
    "FAMNAME" => "test",
    "PLZ" => "test",
    "ORT" => "test",
    "STRASSE" => "test",
    "EMAIL" => "test",
    "PRIVTEL" => "test"
  ),
  "KDNR" => 2,
    "GESCHL" => "test2",
    "TITEL" => "test2",
    "VORNAME" => "test2",
    "FAMNAME" => "test2",
    "PLZ" => "test2",
    "ORT" => "test2",
    "STRASSE" => "test2",
    "EMAIL" => "test2",
    "PRIVTEL" => "test2"
  ),
  etc...
);

I want to write the array tbl with 3070 arrays to a xlsx file. I use for this PhpSpreadsheet.

This is my php code:

<?php
//call the autoload
require($_SERVER['DOCUMENT_ROOT'].'/src/phpspreadsheet/vendor/autoload.php');
//load phpspreadsheet class using namespaces
use PhpOffice\PhpSpreadsheet\Spreadsheet;
//call iofactory instead of xlsx writer
use PhpOffice\PhpSpreadsheet\Aligment;
use PhpOffice\PhpSpreadsheet\Fill;
use PhpOffice\PhpSpreadsheet\IOFactory;


//load from xlsx template
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($_SERVER['DOCUMENT_ROOT']. '/src/ExcelVorlagen/polbezirk_template.xlsx');

//loop the data
$contentStartRow = 3;
$currentContenRow = 3;

//set coulm dimension to auto size
$spreadsheet->getActiveSheet()
            ->getColumnDimension('A')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('B')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('C')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('D')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('E')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('F')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('G')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('H')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('I')
            ->setAutoSize(true);
$spreadsheet->getActiveSheet()
            ->getColumnDimension('J')
            ->setAutoSize(true);

xdebug_break();
foreach($tbl as $item){
    //insert a row after current row (before current row + 1)
    $spreadsheet->getActiveSheet()->insertNewRowBefore($currentContenRow + 1,1);

    //fill the cell with Data
    $spreadsheet->getActiveSheet()
        ->setCellValue('A'.$currentContenRow, $item['KDNR'])
        ->setCellValue('B'.$currentContenRow, $item['GESCHL'])
        ->setCellValue('C'.$currentContenRow, $item['TITEL'])
        ->setCellValue('D'.$currentContenRow, $item['VORNAME'])
        ->setCellValue('E'.$currentContenRow, $item['FAMNAME'])
        ->setCellValue('F'.$currentContenRow, $item['PLZ'])
        ->setCellValue('G'.$currentContenRow, $item['ORT'])
        ->setCellValue('H'.$currentContenRow, $item['STRASSE'])
        ->setCellValue('I'.$currentContenRow, $item['EMAIL'])
        ->setCellValue('J'.$currentContenRow, $item['PRIVTEL']);
    //increment the current row number
    $currentContenRow++;                 
}

//remove last empty rows
//$spreadsheet->getActiveSheet()->removeRow($currentContenRow,2); 

//set the header first, so the result will be treated as an xlsx file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

//make it an attachment so we can define filename
header('Content-Disposition: attachment;filename="result.xlsx"');

//create IOFactory object
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
//save int php output
$writer->save('php://output');

when i execute the code it takes 49 minutes to create the xlsx file, this it to long for only 3070 rows. Is there a faster way? Or have i a bottleneck in my code?

hope you guys can help me

best best regards

like image 852
Pesi Avatar asked Oct 16 '25 04:10

Pesi


1 Answers

thank you very much kalyfe, i modified my code like this:

$row = count($tbl);

$spreadsheet->getActiveSheet()->insertNewRowBefore($currentContenRow + 1, $row);

foreach($tbl as $item){
    //fill the cell with Data
    $spreadsheet->getActiveSheet()
        ->setCellValue('A'.$currentContenRow, $item['KDNR'])
        ->setCellValue('B'.$currentContenRow, $item['GESCHL'])
        ->setCellValue('C'.$currentContenRow, $item['TITEL'])
        ->setCellValue('D'.$currentContenRow, $item['VORNAME'])
        ->setCellValue('E'.$currentContenRow, $item['FAMNAME'])
        ->setCellValue('F'.$currentContenRow, $item['PLZ'])
        ->setCellValue('G'.$currentContenRow, $item['ORT'])
        ->setCellValue('H'.$currentContenRow, $item['STRASSE'])
        ->setCellValue('I'.$currentContenRow, $item['EMAIL'])
        ->setCellValue('J'.$currentContenRow, $item['PRIVTEL']);
    //increment the current row number
    $currentContenRow++;                 
}

now it takes 15sec for creating the xlsx file

like image 188
Pesi Avatar answered Oct 18 '25 19:10

Pesi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!