My website has a functionality of downloading excel sheet. I'm using PHPExcel library with Codeigniter to create the .xlsx sheet. It works fine almost everywhere but in iPad/ iPhone Safari. The following error covers the screen.
Safari Error Screenshot
My code to download the file is following:
ob_end_clean();
$filename='attendance_list.xlsx';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
$objWriter->save('php://output');
And i am calling the php function with this code via jquery and AJAX. Please provide me with the answers.
instead of:
header('Content-Type: application/vnd.ms-excel');
use
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
Related
Via cURL I downloaded a pdf in byte form. After this I processed the cURL response
header('Cache-Control: private');
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $curlResponse
The downloaded PDF is not compatible with PDF-A standards.
Is there a way to achieve compatibility with PDF-A?
I tried searching in the documentation of FPDF and TCPDF but did not find any function for achieving this.
I am using PHPExcel library for downloading excel sheet in CodeIgniter framework. Its working fine in local server, its downloading excel sheet file, opening and showing correct data, now when I run that same code in live server it is downloading, when I open that file, it is giving this error, The file you are trying to open 'filename.xsl' is in a different format than specified by the file extension. verify that the file is not corrupted and is from a trusted source before opening the file.
Here is the code.
$this->excel->createSheet();
$this->excel->setActiveSheetIndex(2);
$filename='Monthly Report'.date("m-d-Y, h:i:s").'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');
$objWriter->save('php://output');
I also used the method ob_end_clean(); before $objWriter, still got the same error. Any help will be appreciated.[it's in a live server on the local server it's working fine]
ob_end_clean();
ob_start();
Used the above lines of code before $objWriter->save('php://output');
and it worked.
I am trying to export an export file in PHP. I am using Codeigniter Framework.
I exported Excel in this way:
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename={$filename}.xls");
It is showing data in Windows in right format. But prompt a message when I initially open it. But When I open it in MAC OS, it is not working.
How can I export Excel in right format that works in Google Drive as well? I have tried so many ways as above and as follows. All not working properly in Mac and Google Drive spreadsheets:
header('Content-Disposition: attachment; filename='.$filename.'.xlsx');
header('Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename='.$filename.'.xls');
header('Content-type: application/vnd.ms-excel');
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
Even though you're using codeigniter I would advise you to load a external library to handle your excel stuff.
There's library that everyone uses with PHP called phpspreadsheet.
https://phpspreadsheet.readthedocs.io/en/latest/
This will handle all the problems you're having and more. It's really easy to install since its available as a composer package.
https://packagist.org/packages/phpoffice/phpspreadsheet
Creating a excel file would be as simple as:
<?php
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
And since your using codeigniter you can combine this with the CI download helper to force download the document.
Try This
ini_set('memory_limit', '-1');
//ini_set('MAX_EXECUTION_TIME', '-1');
ini_set('max_execution_time', 300);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=Registration.xlsx");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: application/xls");
header("Content-Disposition: attachement; filename=export_test.xls");
When I run the function below it locates and reads the file, displaying the results in the my Chrome Dev Tools preview tab correctly in the csv format. But it's not downloading it. If I link directly to the file in my browser it downloads it, so it doesn't appear to be an .htaccess issue. I've used the example in the documentation and many variations of it found here on Stack Overflow but the results are the same: the file displays in my preview tab in dev tools (and the same goes with Firefox as well) but no download. My code:
public function download()
{
$file = $this->dir;
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='. $file);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
}
I'm developing locally with latest Wamp server. When I push/pull to my remote, the result is the same.
From your question, it sounds like you might be trying to download your file via an AJAX request.
If so, I don't believe you can do this. Instead you could open the link to the file in a new window, which will successfully download the file.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
On my local system above code work properly but on server its not work.
I don't know yet if it will help you, but if you wan't, you can generalize octet-stream output for some extension, directly in VirtualHost config.
For that, use AddType application/octet-stream .xlsx .xls
And all url to Excel files would be downloaded.
write ob_clean() just before the last line below , i was having same problem got resolved by this
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="temlik.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
ob_clean();
$objWriter->save('php://output');