Runshax
Runshax

Reputation: 117

How to save multiple file in phpexcel?

For example I have 5000 data. I'm trying to split these data 1000 for each file in excel.

Saw this example :

Create two workbooks

($objPHPExcelFile1=new PHPExcel(); $objPHPExcelFile2=new PHPExcel();

so I did this

    $jco=1; $jcos=1;
    $trig=0; $rowNya = 2;
    $no = 0;


if ($VinDB->num_rows($result) != 0)
{

    while($line = $VinDB->fetch_array($result)){

        if($trig ==0 )
        {   
            $objPHPExcel[$jcos] = new PHPExcel();
            $objPHPExcel[$jcos]->getProperties()->setCreator("Test")
            ->setLastModifiedBy("Test")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("result file");

            $trig=1;

        }

        $no = $no +1;
        if($jco <= 1000){
        $objPHPExcel[$jcos]->setActiveSheetIndex(0)
            ->setCellValue("A$rowNya", $no)
            ->setCellValue("B$rowNya", @$line["NoTransaksi"])
            ->setCellValue("C$rowNya", @$line["Pesan"])
            ->setCellValue("D$rowNya", @$line["Jam"])
            ->setCellValue("E$rowNya", @$line["Alias"])
            ->setCellValue("F$rowNya", @$line["Nomor_hp"])
            ->setCellValue("G$rowNya", @$line["GroupKU"])
            ->setCellValue("H$rowNya", @$line["Report"])
            ->setCellValue("I$rowNya", @$line["Status"])
            ->setCellValue("J$rowNya", @$line["Token"]);
            $rowNya = $rowNya + 1;
        $jco++; 
        }

        else{
        $jco=1;
        $trig=0;

            $objPHPExcel[$jcos]->getActiveSheet()->setTitle('Simple');
            $objPHPExcel[$jcos]->setActiveSheetIndex(0);


            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="Report_part'.$jcos.'.xls"');
            header('Cache-Control: max-age=0');

            $objWriter[$jcos] = PHPExcel_IOFactory::createWriter($objPHPExcel[$jcos], 'Excel5');
            $objWriter[$jcos]->save('php://output');

        $jcos++;

        }
    }
}

It worked for the first 1000 data there's a save button, but for the part 2 its not working. Can anyone help me. Here the screenshot:

enter image description here

Latest: Ok so ..i cant upload multiple files to browser, so i just do multiple sheet and save it on my local disk, but how do i download it from my localhost/server

enter image description here

here's my code

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('/xampp/htdocs/sms/Report_'.$jcos.'.xls');
$file = 'sms/Report_'.$jcos.'.xls';
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$file.'"');
header('Content-Length: '.filesize($file));
readfile($file); 

Upvotes: 2

Views: 5361

Answers (1)

RiWe
RiWe

Reputation: 377

You can't send multiple files to the browser. Save your workbooks to disk, pack them into one file (e.g. zip) and send this single file to the browser.

Edit

After your while loop you can add:

$zip = new ZipArchive();
$zip->open('/xampp/htdocs/sms/Reports.zip', ZipArchive::OVERWRITE);
$zip->addGlob('/xampp/htdocs/sms/Report_*.xls');
$zip->close();

Please have a look at the docs for more options.

at last:

$file = '/xampp/htdocs/sms/Reports.zip';
header('Content-type: application/zip'); // Please check this, i just guessed
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: '.filesize($file));
readfile($file);

Upvotes: 3

Related Questions