Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel save file

Tags:

php

phpexcel

Hi all I have a site and i want to create an excel file and save into my folder into my server. I have tried in this mode but every time ask me to download it. I don't want that ask to download because after I have to create many xls is a report and isn't necessary to download but only to save into a folder. This is my code:

require_once 'inc/PHPExcel.php';

// Create new PHPExcel object
$objPHPExcel = new PHPExcel();

// Set document properties
$objPHPExcel->getProperties()->setCreator("Alessandro Minoccheri")
                             ->setLastModifiedBy("Alessandro Minoccheri")
                             ->setTitle("Office 2007 XLSX Test Document")
                             ->setSubject("Office 2007 XLSX Test Document")
                             ->setDescription("Generazione report inverter")
                             ->setKeywords("office 2007 openxml php")
                             ->setCategory("");


// Add some data
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Hello')
            ->setCellValue('B2', 'world!')
            ->setCellValue('C1', 'Hello')
            ->setCellValue('D2', 'world!');

// Miscellaneous glyphs, UTF-8
$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A4', 'Miscellaneous glyphs')
            ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');

// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('Simple');


// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);


// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

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

I have also tried:

$objWriter->save('namee.xls');

But again ask me to download.

How can I solve this?

like image 411
Alessandro Minoccheri Avatar asked Jun 10 '13 10:06

Alessandro Minoccheri


People also ask

How do I select a location to save an Excel file using Phpexcel?

Change the file name to desired path i.e, $name = '/path/to/folder/xyz. xlsx'; $objWriter->save($name);

How to save Excel file in PHP?

You may use the following code: header('Content-Type: application/vnd. ms-excel'); header('Content-Disposition: attachment;filename="file. xlsx"'); header('Cache-Control: max-age=0'); $writer->save('php://output');


1 Answers

That's because the following headers:

// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');

That's because browsers will open the save as... box if they see the Content-Disposition header. Remove those lines.

However, the excel should being saved on disk anyway. Don't you see it? if not, make sure that PHP has proper permissions to write into that folder

like image 77
hek2mgl Avatar answered Sep 20 '22 03:09

hek2mgl