1

I use PHPExcel to describe a template from Excel with php and then output it.

Problem is that I want to save the file on the desktop but I don't know how to define the path to the desktop?

I would also like to integrate "header" into the file so that the save dialog appears. But this doesn't work if I load a template because I get the error message: "File type doesn't fit".

Here is my existing code:

// Start
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

// Include Class
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

// Template loading
$objPHPExcel = PHPExcel_IOFactory::load('Template.xlsx');

// Define Values
$test = 'Test';

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B5', $test)                 
            ;


// Excel Document save 
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('C:\Users\ ???  \Desktop\Data.xlsx');

Thanks for help

user9554121
  • 17
  • 1
  • 7

2 Answers2

0

you missed a couple of things, you need to specify the fileType and an object createReader to passe to the load

// Start
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

// Include Class
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

// Template loading
$fileType = 'Excel2007'; // use 'Excel5' for .xls files
$fileName = 'Template.xlsx';

$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

// Define Values
$test = 'Test';

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B5', $test)                 
            ;


// Excel Document save with header
$xlsName = 'Document';          
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
//header('Content-Type: application/vnd.ms-excel'); // use the header below instead ( for xlsx files )
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); 

header('Content-Disposition: attachment;filename='.$xlsName.''); // quotes are removed here
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

check this for the save dialog : bring up save as dialog within php

Taki
  • 17,320
  • 4
  • 26
  • 47
0

Ok, I have replaced the code. But with the header it doesn't work because I get the message of incompatible file type. The acutual Code is :

// Start
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');

// Include Class
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

// Template loading
$fileType = 'Excel2007'; // use 'Excel5' for .xls files
$fileName = 'Template.xlsx';

$objReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objReader->load($fileName);

// Define Values
$test = 'Test';

$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('B5', $test)                 
            ;


// Excel Document save with header
$xlsName = 'Document';          
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$xlsName.'"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');
user9554121
  • 17
  • 1
  • 7