Simon Delaunay
Simon Delaunay

Reputation: 145

PHPExcel memory error

i convert excel files to HTML table with PHPExcel Reader function.

i'm getting this error : Fatal error: Out of memory (allocated 1827405824) (tried to allocate 106 bytes)

I've changed my memory_limit to 2048M in my php.ini file..

Still getting the error..

So How can i only load the first sheet of my Excel file, it can maybe solve my issue ?

$excel = glob(''.$test.'\\'.$client.'.{xlsx,xls,xlsm,xlsm.ink}',GLOB_BRACE);
$filterSubset = new \PHPExcel_Reader_DefaultReadFilter('A','N');
$objReader = \PHPExcel_IOFactory::createReaderForFile($excel[0]);
$objReader->setReadFilter($filterSubset);

$objPHPExcel = $objReader->load($excel[0]);

## THIS DOESNT WORK ?! ##
$objPHPExcel->setActiveSheetIndex(1);

    $links = $objPHPExcel->getActiveSheet()->getHyperlinkCollection();
    var_dump($links);

    foreach ($links as $link){
        $url = $link->getUrl();
        $url = str_replace('file','mnt', $url);
        $link->setUrl($url);
    }

$writer = \PHPExcel_IOFactory::createWriter($objPHPExcel, "HTML");
$writer->generateStyles();
$writer->generateSheetData();

EDIT :

I do it like that :

    public function showClientAction($client)
{
    $excel = glob(''.$path'.{xlsx,xls,xlsm,xlsm.ink}', GLOB_BRACE);
    $filterSubset = new \PHPExcel_Reader_DefaultReadFilter('A','N');
    $objReader = \PHPExcel_IOFactory::createReaderForFile($excel[0]);
    $objReader->setReadFilter($filterSubset);

    $worksheetList = $objReader->listWorksheetNames($excel[0]);
    $sheetname = $worksheetList[0];

    /**  Advise the Reader of which WorkSheets we want to load  **/

    $objReader->setLoadSheetsOnly($sheetname);

    $objPHPExcel = $objReader->load($excel[0]);

    var_dump($sheetname);

    $writer = \PHPExcel_IOFactory::createWriter($objPHPExcel, "HTML");
    $writer->generateStyles();
    $writer->generateSheetData();

    return $this->render('MonextPerfclientBundle:Default:testexcel.html.twig', array(
        'excelHtml'=>$writer,
        'stylesExcel'=>$writer,
    ));
}

the var_dump($sheetname); output is the good one, it Works great !

But how can i be sure to load only the first sheet ?

It dont seem to be a faster loading.. *scuze my english..

Upvotes: 0

Views: 419

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

So How can i only load the first sheet of my Excel file, it can maybe solve my issue ?

Straight from the PHPExcel Documentation

$inputFileType = 'Excel5'; 
$inputFileName = './sampleData/example1.xls'; 
$sheetname = 'Data Sheet #2'; 

/**  Create a new Reader of the type defined in $inputFileType  **/ 
$objReader = PHPExcel_IOFactory::createReader($inputFileType); 
/**  Advise the Reader of which WorkSheets we want to load  **/ 
$objReader->setLoadSheetsOnly($sheetname); 
/**  Load $inputFileName to a PHPExcel Object  **/ 
$objPHPExcel = $objReader->load($inputFileName); 

If you don't know the name of the first worksheet, then PHPExcel provides a helper method to get a list of the worksheet names without having to load the whole file.

Upvotes: 1

Related Questions