Error XLS file generate and open using html,php - php

I have generate xls file using html and php everthing is fine.
But problem is that when tring to open file then following error display
My code is here:
$filename = "file.xls";
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
header("Content-type: application/x-msexcel; charset=utf-8");
header("Content-Disposition: attachment;filename=".$filename); //tell browser what's the file name
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: max-age=0");
echo '<table>
<tr>
<th>heading1</th>
<th>heading2</th>
<th>heading3</th>
</tr>
<tr>
<td>data1</td>
<td>data2</td>
<td>data3</td>
</tr>
</table>';

It's because of the data you are writing to it. Excel expects a properly formatted file if saving as an .xls. You have to write explicit data reserved to Excel and not just HTML tables.
In order to prevent the popup error window, save your file as .csv and not .xls.
.xls files are "binary" files, as opposed to the data you wish to enter which is "text-based". That is why Excel prompts you when opening the file.
You could even save it with an .html extension, that will work also, I tried it.
One of the libraries you could use is called PHPExcel and can be found at the following URL:
http://phpexcel.codeplex.com/

Related

No image in Exported Report to MS Word and Excel File

I am working on the reporting part of my project, the project contain hundreds of dynamic generated reports and each report contain images and formatted followed by Table. And all these reports need to be downloadable in doc/docx (Ms Word), xls /xlsx(MS Excel) and PDF
For this I want to Convert the Generated HTML report to the corresponding file
for this I am using following code
function export_attendance_view_doc($htmlcontent){
header("Content-Type: application/vnd.msword");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=attendance_report.doc");
echo $htmlcontent;
}
function export_attendance_view_xls($htmlcontent){
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=attendance_report.xls");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=attendance_report.doc");
echo $htmlcontent;
}
Using the above code I am getting the doc and xls file but the issue is that the image is not rendering in the file if the script is run in live server working fine in localhost if I am using full url in <img src not displaying image in local if using relative path. Same in the case of Excel file.

Transferring Excel via PHP header()

An Excel file is stored in a database (hexvalues of former binary data). I need to read it and make it available for download.
Here's the code...
$out = hex2bin($out); // the stuff from the database
header("Content-Type: application/vnd.ms-excel");
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename='$filename'");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
die($out);
Actually I receive stuff... and it looks like the original file with a slight difference.
When using a hex-editor, it shows
ÐÏࡱá
instead of
ÐÏࡱá
(looks like an additional tab). So Excel isn't able to show the sheet but the raw data.
That's not from the data in $out; the conversion bin2hex (during import in database) and hex2bin (export from database) works without problems. If I write $out to a file via fopen / fwrite, it's exactly the original Excel file (which I can open normally).
How can I accomplish this?
Ensure you don't have this extra tab character before the opening <?php tag or after the closing one ?>. Actually, it is good practice to omit the closing tag exactly for this reason.

how to create excel file and read xls using php excel reader?

Create excel file dataexcel.xls using php. dataexcel.xls file has more data.
Then same dataexcel.xls file doesn't read php excel reader. Directly created .xls files are readable. But created excel file is not readable.
I have used http://sourceforge.net/projects/phpexcelreader excel reader class.
header("Content-type: application/x-msdownload");
header("Content-Disposition: attachment; filename=dataexcel.xls");
header("Pragma: no-cache");
header("Expires: 0");
Any header content change?

add file to joomla

hello i am using joomla and i am trying to create an option that the user will be able to download a csv or excel file of the table that is currently presented.
i am trying to use PHPExcel for the creation of the file.
I have created an export.php file:
{
$objXLS = new PHPExcel();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=File.xls");
header("Content-Transfer-Encoding: binary ");
$objSheet = $objXLS->setActiveSheetIndex(0);
//$objSheet->setCellValue(cell, value);
$objSheet->setCellValue('A1', '1');
$objSheet->setCellValue('A2', '2');
$objSheet->setCellValue('A3', '3');
$objSheet->setCellValue('A4', '4');
$objSheet->setCellValue('A5', '5');
$objSheet->setCellValue('B5', date('H:i:s'));
$objXLS->getActiveSheet()->getColumnDimension("A")->setAutoSize(true);
$objXLS->getActiveSheet()->getColumnDimension("B")->setAutoSize(true);
$objXLS->getActiveSheet()->setTitle('Test Stats');
$objXLS->setActiveSheetIndex(0);
$objWriter = PHPExcel_IOFactory::createWriter($objXLS, 'Excel5');
//$objWriter->save('php://output');
exit;
}
}
the file gets the info of the table, inserts it into an excel sheet and outputs it to download but i am not able to access this file due to the joomla restrictions.
when i try to input the code to my model and call it the file that comes out is the entire page or the file is printed in the site page as garbage.
how do grante access to this file so i can open it in a new tab or something.
or is there a simpler way to export this information to an excel file or csv using AJAX or Jquery?
thank you
Your problem most likely is that the Joomla framework will output some additional HTML which is not needed for this special case; I think what you're trying to do is pretty similar to what was asked in this question. The gist is that you have to create your own action in your controller, and using and additional format=raw parameter in your URL.

PHP_excel: Export to xlsx gets downloaded successfully but when we open it it shows warning Excel found unreadable content

i am using php_excel to export to xlsx. In my application im making use of template.
When i download, the xlsx file gets downloaded fine, but when we open its showing the following warning:
"Excel found unreadable content in 'project_report(3).xlsx'. Do you want to recover the contente of this workbook? If you trust the source of this workbook, click Yes."
If i click yes it opens the file correctly.
And one more thing is when i attach the downloaded file to the mail. And if i open it sing Google Spread sheet it says bad format unable to open.
so if anyone know the reason please suggest me to solve this.
check your headers, here are mine:
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$filename");
header("Content-Transfer-Encoding: binary");
$objWriter->save('php://output');
check for output (spaces before <?php forgotten echo? etc

Categories