Sergey Scopin
Sergey Scopin

Reputation: 2245

PHPexcel can't save file

I have a file where I want to use standart example. Like here , but I added php://output instead of str_replace('.php', '.xlsx', __FILE__) because I want file to be downloaded by user. So, I have a form:

$res.="<form method='POST' action='".$this->self()."'>";
$res.="<input type='text' class='span5' placeholder='Address' name='excelAddress'></input>";
$res.="<button type='submit' id='export' class='span2 pull-right'>Экспорт</button>";
$res.="</form>";

And if post['excelAddress'] isset I try standart example:

$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Administrator");
$objPHPExcel->getProperties()->setLastModifiedBy("Administrator");
$objPHPExcel->getProperties()->setTitle("Показания клиентов по адресу:".$_POST['excelAddress']);
$objPHPExcel->getProperties()->setSubject("Показания клиентов по адресу:".$_POST['excelAddress']);
$objPHPExcel->getProperties()->setDescription("Показания клиентов по адресу:".$_POST['excelAddress']);

$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('B2', 'world!');
$objPHPExcel->getActiveSheet()->SetCellValue('C1', 'Hello');
$objPHPExcel->getActiveSheet()->SetCellValue('D2', 'world!');

$objPHPExcel->getActiveSheet()->setTitle('Simple');

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('php://output');

Also in the top of my page I have includes:

/** PHPExcel */
include './lib/PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include './lib/PHPExcel/Writer/Excel2007.php';

Also I have there:

ini_set("display_errors", "On");
error_reporting(E_ALL ^ E_NOTICE);

Do I need:

/** Include path **/
ini_set('include_path', ini_get('include_path').';../Classes/');

Or it just not to write ./lib./... where I have all libs? When I try my example I just have:

KL�,EG�D�X�[Content_Types].xml��MN�0���"�%nY ��vAa  �(0����ؖg�w{&i�@�nbE�{��y��d۸l m������X�(���)���F���;@1_������c)j�x/%��E��y� �QĿi!��K�y3�J<���Z1�0?Y�L%zV c��Ib7�����a/l٥P1:�q�r��j��j0A����u�""���(�   ���W�M��)Tj�({ܲ�>�O��,X٭���>B��~׭���Ӥ6�J=�oBZ����t��X4���Cg�,���QgmrL�ٯc�e���t��    Z�?����hPv��±��u�j���R�������}�Cv��PKL�,E�78�K_rels/.rels���j�0��{ �{���1F�^ʠ�2��l��$���-}�y����Î��O��v�y�;�؋Ӱ.JP���޵^������Yű�3G�Ww�g)����>�qQC��D����b!�]�i$L��3����2n���oT�:Z �h�����[��4�ი��]��yN�,ە�>�>�j -' V�)�#��EF^6��n���8q"K��H��>_ׄ����eƏ�<⇄�Ud�v�� T�PKL�,E�$�V��xl/_rels/workbook.xml.rels���j�0D�� ��ZvZJ)�s)�\[����LlIh7m��UH��Ĭؙ�H���8�OL���*J�M���|4��g��[=� 

What is my mistake?

Upvotes: 0

Views: 7344

Answers (2)

Debflav
Debflav

Reputation: 1151

I guess that you forgot to add header.

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment;filename={$filename}");
header('Cache-Control: max-age=0');
$objWriter->save('php://output');

In addition headers are for :

.xls: application/vnd.ms-excel

.xlsx: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

Upvotes: 2

Mark Baker
Mark Baker

Reputation: 212412

You need to send the http response headers so that the browser knows how to handle the data that is being sent to it. Take a look at /Examples/01simple-download-xlsx.php

Upvotes: 3

Related Questions