PHPExcel save file in a folder or open excel - php

I am using PHPExcel to export data from my PHP page to Excel.
I am using Excel5.
I want the excel file to be be saved a particular folder that is specified in the code
OR better still,
I want Excel to open with the data written in it so that user can save it wherever he wants.
What should I do.
Please guide me
Pre

I solved this problem doing this:
$objWriter->save(str_replace(__FILE__,'/path/to/save/filename.extension',__FILE__));
In my case, it worked!

This will ask the user to save / open the file:
$excel = new PHPExcel();
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_name.xls"');
header('Cache-Control: max-age=0');
// ...
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel5);
$writer->save('php://output');

Look at 01simple-download-xls.php in the PHPExcel Tests directory. This sends the Excel file to the user's browser, which then prompts them to either display it (in Excel if they have it installed, or other spreadsheet program if they have the extension associated with LibreOffice Calc or Gnumeric or whatever), or save it to their local disk.

This solved this issue for me:
$this->objWriter->save(str_replace(__FILE__,$_SERVER['DOCUMENT_ROOT'] .'/path/to/save/filename.xlsx',__FILE__));

Related

How to export csv file from a particular location and made available for download?

Due to Speed issues and threading I have generated a CSV file in a particular location
Admin - CSV - xyz.csv
I want to write a program to just made it available on view. I know how to make a tmp csv and use header function for making downloadable the csv, But I am looking for the approach which only take this xyz.csv available for download on a click. Please Help!
I read out lot of examples but not find exact answer for my question.
As per recommended I have tried.
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="data.csv"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'csv');
$objWriter->save('php://output');
exit;

Warning after download and open Excel file exported from Magento

I using exporting data to excel 2007 using PhpExcel 1.8.0. Its working but
Excel File is saved in Client side but when I open the downloaded Excel file, I get the warning message
We found a problem with some content in 'filename.xlsx'. Do you want
us to try to recover as much as we can? If you trust the source of
this workbook, click Yes.
I am using PhpExcel library in Magento.
Could anyone help me to solve this problem?
Code is below:
$objPHPExcel->getActiveSheet()->setTitle($store_name." Product List");
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header("Content-Disposition: attachment; filename=".Mage::getBaseDir().'/media/productpricinglist/'.$store_name.'product_list.xlsx');
header("Cache-Control: max-age=2");
ob_clean();
$objWriter->save("php://output");
I had this problem too. I just added exit; at the end of my PHP script

Error while opening an Excel file that generated using phpExcel

I am trying to generate an Excel file using phpExcel library in Codeigniter.
The problem is that while downloading the file it shows characters in in undifined formats
While opening the downloaded file it shows the message that is an different format, please refer the pic
and while opening the file content as
and my code stuff is:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->load->library('excel');
$sheet = new PHPExcel();
$objWorkSheet = $sheet->createSheet();
$sheet->getProperties()->setTitle('JDI Problem Improvement Strip')->setDescription('JDI Problem Improvement Strip');
$sheet->setActiveSheetIndex(0);
$sheet_writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$sheet_writer->save('php://output');
Are you creating Excel 2007 files? If so try using mimetype application/vnd.openxmlformats-officedocument.spreadsheetml.sheet and file extension .xslx
refer - PHP Excel - The file you are trying to open .xls is in a different format than specified by the file extension
ob_end_clean();
ob_start();
It cleans the buffer and get correct output. At least for me it resolve the issue.

Issue with PHPExcel 1.7.9, 2013-06-02 [duplicate]

I am trying to generate an Excel file using phpExcel library in Codeigniter.
The problem is that while downloading the file it shows characters in in undifined formats
While opening the downloaded file it shows the message that is an different format, please refer the pic
and while opening the file content as
and my code stuff is:
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$this->load->library('excel');
$sheet = new PHPExcel();
$objWorkSheet = $sheet->createSheet();
$sheet->getProperties()->setTitle('JDI Problem Improvement Strip')->setDescription('JDI Problem Improvement Strip');
$sheet->setActiveSheetIndex(0);
$sheet_writer = PHPExcel_IOFactory::createWriter($sheet, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="01simple.xls"');
header('Cache-Control: max-age=0');
$sheet_writer->save('php://output');
Are you creating Excel 2007 files? If so try using mimetype application/vnd.openxmlformats-officedocument.spreadsheetml.sheet and file extension .xslx
refer - PHP Excel - The file you are trying to open .xls is in a different format than specified by the file extension
ob_end_clean();
ob_start();
It cleans the buffer and get correct output. At least for me it resolve the issue.

Download with smartphone generate xls file with PHPExcel

I have a big problem when I want to download on my smart phone a xls file generated by PHPExcel it me download a php file named my page, while on my pc it load properly.
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition:inline;filename=fichie.xls');
$writer->save('php://output');
exit();
There's two problems with your line that sets the content disposition.
It should have spaces between the words, which is a standard requirement for headers.
It should be attachment if you want the file to download, as opposed to display inline.
e.g.
header('Content-Disposition: attachment; filename=fichie.xls');

Categories