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

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?

Related

Error XLS file generate and open using html,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/

How to save html table in Excel

I have been trying to save html data in excel format(export to excel functionality basically) in PHP. I achieved that by changing header to
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$filename");
header("Pragma: no-cache");
header("Expires: 0");
But on opening the file, it shows a warning for above excel 2003 applications. So can I save the excel in 2008 or 2010 format just by modifying the header or do I have to go for other frameworks providing this feature like "PHPExcel" or something. And yes, I am using CodeIgniter for my website
Thanks in advance.
You need to use some libraries to generate Excel tables.
Standart HTML output is not converted automatically to the specified format if you only set the Content-Type you want.
Try changing the MIME type to:
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
instead of
application/vnd.ms-excel

How to convert PHP CSV output into excel with rich font styles and CSS in PHP

I tried to put font styles and color in EXCEL XLS sheet generation from CSV. Even i explored lots of libraries to get Excelsheet with enriched format and style.
Is there any libraries by which I can get formatted Excel XLS sheet from CSV output in PHP
I usually use phpexcel. It supports xlsx as well as just xls formats.
You can generate a flat web page with a table that has your font styles. Then download the table in excel using the code below.
header("Pragma: public");
$display="attachment";
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: $display; filename=$filename");
header("Cache-Control: no-cache");
I'd recommend good old http://pear.php.net/package/Spreadsheet_Excel_Writer/
require_once 'Spreadsheet/Excel/Writer.php';
$table = new Spreadsheet_Excel_Writer();
$table->setTempDir($files_base.'/tmp');
$font= 'HelmetCondensed';
$f_std =& $table->addFormat();
$f_std->setFontFamily($font);
$f_b_c =& $table->addFormat();
$f_b_c->setFontFamily($font);
$f_b_c->setBold();
$f_b_c->setAlign('center');;
$table->write(0, 0, 'normal', $f_std);
$table->write(0, 1, 'bold+centered', $f_b_c);
// send HTTP headers
$table->send('NAME.xls');
// Let's send the file
$table->close();

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