Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35973

PHPExcel save file

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?

Upvotes: 7

Views: 47738

Answers (1)

hek2mgl
hek2mgl

Reputation: 157967

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

Upvotes: 10

Related Questions