How to save html table in Excel - php

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

Related

Formatting Excel when exporting via header

Personally I've never used this method before and I havent been able to find any information to help me.
The page I'm updating uses the following to export a html table as an Excel file.
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Target-Report.xls");
header("Pragma: no-cache");
header("Expires: 0");
It generates the file just fine, I just need to make the report more presentable.
Is there a way to set Cell type in Excel (i.e. Percentage) and set decimal places (in cells where I've created excel formulas).
I'd prefer to use PHPExcel, but I dont have time to practically rewrite the entire script

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();

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?

How can I view/open a word document in my browser using with PHP or HTML

How can I open and view a .doc file extension in my browser? The file is located on my server.
Two options: First is to just link to it, e.g. My Word Document, the second is to use an iframe and point it to the document. For this to work, however, most browsers require that the server sends a Content-disposition: inline header with the document. If you cannot configure your web server to do this, you can wrap the document in a bit of php:
<?php
header('Content-disposition: inline');
header('Content-type: application/msword'); // not sure if this is the correct MIME type
readfile('MyWordDocument.doc');
exit;
And then link to that script instead of your word document.
This isn't guaranteed to work though; the content-disposition header is just a hint, and any browser may choose to treat it as an attachment anyway.
Also, note that .doc isn't exactly portable; basically, you need Word to display it properly (Open Office and a few other Open Source applications do kind of a decent job, but they're not quite there yet), and the browser must support opening Word as a plugin.
If the .doc file format requirement isn't set in stone, PDF would be a better choice (the conversion is usually as simple as printing it on a PDF printer, say, CutePDF, from inside Word), or maybe you can even convert the document to HTML (mileage may vary though).
…
You will need a browser with a plugin for Office documents installed. I believe Microsoft Office will install one for at least Internet Explorer by default.
If you want to work without a plugin, then you will need to convert the document to another format — HTML for maximum compatibility. This isn't a trivial operation, especially for complex documents (or even those which just contain images).
$file = "$file_name.doc";
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type:application/zip"); // Send type of file
$header="Content-Disposition: attachment; filename=$patient_name.zip;"; // Send File Name
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
#readfile($file);
You can use google docs instead as it is free and reliable
You can assign your file path to iframe.
e.g. iframe1.Attributes.Add("Src", "http://docs.google.com/gview?url=http://YOUR_FILE_PATH&embedded=true");
If your .doc file is accessable online, you can try Office Web Viewer service.
If your documents stored in Intranet, you can use Microsoft Office Web Apps Server. It allows users to view Word, PowerPoint, Excel documents via browser.
//Edit
$header="Content-Disposition: attachment; filename=$file_name.doc;"; // Send File Name

Firefox : Open XLSX file not saving file butn opening binary

I generate a file server side and I want the client to automatically open it : it's a XLSX file. Firefox just opens the file and I see the binary content of the XLSX file in the browser, but I want it to be open via a Save As... box.
It works fine in Chrome with the same code (it saves it) but not firefox...
Any ideas ?
Have a look at this - Php exec and return binary
Are you sending proper headers??
something like
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfile.xlsx\"");
UPDATE
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename=example.xlsx');
header('Pragma: no-cache');
echo file_get_contents("/path/to/yourfile.xlsx");
UPDATE 2
Spread sheet mime types
application/vnd.ms-excel [official]
application/msexcel
application/x-msexcel
application/x-ms-excel
application/vnd.ms-excel
application/x-excel
application/x-dos_ms_excel
application/xls
UPDATE 3
Regarding your javascript problem did you try using
location.href instead of window.open ??
You need to ensure you are sending this mime type as the Content-Type header:-
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
So you need to map the .xslx extension to this mime type on the server

Categories