简体   繁体   中英

Selective download with PHPExcel

I'm using PHPExcel to download some data stored in MySQLi. I made an algorithm that is working for every data base (in theory). I have tested it with some of them and it was working fine.

I extracted names of the columns in an array: column_names and then, I'm adding titles and data to the excel report.

    // Adding titles
    $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A1',$bigTitle);

    $counter = 0;
    $let = 'a';
    while ($counter <= count($column_names)){
        $objPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue(strtoupper($let).'3',  $column_names[$counter]);
        $let++;
        $counter++;
    }


    //Adding data
    $i = 4;
    while ($row = $result->fetch_array()) {
        $counter = 0;
        $let = 'a';
        while ($counter <= count($column_names)){
            $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue(strtoupper($let).$i, $row[$column_names[$counter]]);
            $let++;
            $counter++;
        }
        $i++;
    }

I'm connecting to the database using

$conexion = new mysqli('localhost','user','pass','SAT_dbname',21);

I cloned "SAT_db1" database to "SAT_db2". They have exactly the same structure but different information. The download is working if I'm using

$conexion = new mysqli('localhost','user','pass','SAT_db1',21);

But it's not working if I'm using

$conexion = new mysqli('localhost','user','pass','SAT_db2',21);

I don't know what is wrong if they're the same with different names. Is not PHPExcel working with cloned databases? What else could it be?

The error shows up in the browser as "File not found".

EDIT I was testing the download all day and I finally found something: I can download when I have few registers. When I have a few more, I can't.

I'm sending the file to the browser:

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="Report.xlsx"');
    header('Cache-Control: max-age=0');

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

Still haven't found a solution.

PHPExcel has limits with cache. I had to change these limits manually. I used this code before creating PHPExcel object:

set_time_limit(0) ;

$cacheMethod = PHPExcel_CachedObjectStorageFactory:: cache_to_phpTemp; 
$cacheSettings = array( 'memoryCacheSize' => '500MB');
PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);

I assume if you have bigger files, you can expand "memoryCacheSize".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM