This following coding working on localhost perfectly for force to download excel file, but doesn't working on web server.
$par_name="worker_report";
$nwdatetime=date('d-m-Y h-i-s');
$fi = $par_name."_". $nwdatetime;
$extns=".xls";
header('Content-Type: application/force-download');
header('Content-disposition: attachment; filename='.$fi.$extns);
header("Pragma: ");
header("Cache-Control: ");
$_REQUEST['datatodisplay'];
Change the content type to Content-Type: application/vnd.ms-excel
Or try this code if works.
$par_name="worker_report";
$nwdatetime=date('d-m-Y h-i-s');
$fi = $par_name."_". $nwdatetime;
$extns=".xls";
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename='.$fi.$extns);
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$_REQUEST['datatodisplay'];
application/vnd.ms-excel is for .xls files created with the Excel5 Writer.
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet is used for .xlsx files created with the Excel2007 Writer
So you have to check what are you doing there
header('Content-Type: application/vnd.ms-excel'); is for .xls files
header('Content-Type: application/vnd.openxmlformatsofficedocument.spreadsheetml.sheet'); is for .xlsx files
Related
I know PHPExcel is deprecated and replaced with PHPSpreadsheet. I am planning to migrate in the near future but would like to address this problem anyway.
When using MS Office 2016 Office Excel on a Windows 10 Pro environment I get this message:
We found a problem with some content in 'Myfile.xlsx'. Do you want us to try to recover as much as we can?
Click Yes
Microsoft Excel was attempting to open and repair the file. To start this process again, choose Open and Repair from the Open file dialog.
Code works fine using LibreOffice. Also if I open the file in LibreOffice and save it with a different filename then it opens fine in MS Excel. Below is my code snippet, is there anything in this code that would cause this to happen.
Thank you
$filename = "Myfile.xlsx";
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, "Excel2007");
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment;filename="'.$filename.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter->save('php://output');
Edit:
Snippet how rows and columns are built
if($data_type=="float" || $data_type=="decimal" || $data_type=="numeric"){
$this->excel->getActiveSheet()
->getStyle($cell)
->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$this->excel->getActiveSheet()
->setCellValueExplicit($cell, $this_val, PHPExcel_Cell_DataType::TYPE_NUMERIC);
}
else{
$this->excel->getActiveSheet()
->setCellValueExplicit($cell, $this_val, PHPExcel_Cell_DataType::TYPE_STRING);
}
If I change from XLSX to XLS
From this
$filename = "Myfile.xlsx";
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, "Excel2007");
To this
$filename = "Myfile.xls";
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, "Excel5");
It works ok. It only fails if using Excel2007
My app (php/phpExcel) provides a download function for a .csv file. When I download the file with chrome or IE all is ok, but when I use Firefox for the download it changes the fileextension to ".csv.htm", then Excel asks how to open because the extension and the fileformat wont fit together.
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename="csvdownload_'.date("Y-m-d_H_i_s").'.csv');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'CSV');
$objWriter->setDelimiter(';');
$objWriter->setEnclosure('"');
$objWriter->setLineEnding("\r\n");
$objWriter->save('php://output');
Use the headers shown below and all browsers should be happy and download the file as a CSV. If you haven't calculated the number of bytes simply leave that header out.
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-disposition: attachment; filename="Your_File_Name_Here.csv"');
header('Content-length: ' . $FileBytes);
header('Content-type: application/octetstream');
header('Pragma: no-cache');
header('Expires: 0');
I am trying to export one survey report into Excel sheet using php and it works fine at my localhost but when I hosted it is seemed to be just showing excel sheet instead of downloading. And I think it overrides the header in my code.
My code is here.
<?php
ob_start();
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=".$filename);
How do I solve this problem with header.
Try following code
// Redirect output to a client’s web browser (Excel2007)
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=".$filename);//filename with extension .xlsx
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
OK, now I read examples of phpexcel. As I understand there shouldn't be any other output on page, to make proper xls-file. I think so, because when I have
echo "some output and my form for query";
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator("Administrator");
$objPHPExcel->getProperties()->setLastModifiedBy("Administrator");
$objPHPExcel->getProperties()->setTitle("test");
$objPHPExcel->getProperties()->setSubject("test");
$objPHPExcel->getProperties()->setDescription("test");
$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 = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
// 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');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
echo"some other code";
And get xls, it has warnings that it doen't have css links there and moreover it puts a lot of text except Hello world!, such as links in excel file.
It looks like this:
I can put all work with xls in other php file and get it with ajax, but how can I give results of query to this file? All queries should be in my main file, because I don't want to have multiple connections to database. How it is clear, what I want. Any advices?
To download xls(x) file, you have to set right headers and clean the buffer to avoid bad stream.
As documentation explains you can try with this code:
//------------- Enter your code before this line --------- //
// Cleaning buffer
ob_clean();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
/* Or you can use
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
*/
// redirect output to client browser
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="your_filename.xls"');
/* Use this for Writer Excel2007
header('Content-Disposition: attachment;filename="your_filename.xlsx"');
*/
header('Cache-Control: max-age=0');
//Start Download
$objWriter->save('php://output');
You can refer here for the Developers Documentation of PHPExcel
what i need :
i just need data to be save in xlsx format.
problem im facing:
return xml reponse
here is the response im getting url: http://postimg.org/image/y29ppo2cd/
here is my header information:
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="simple.xlsx"');
header('Cache-Control: max-age=0');
//// If you're serving to IE 9, then the following may be needed
//header('Cache-Control: max-age=1');
//// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-reva`enter code here`lidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;