Problem with php exported xls file in Open Office - php

I am using PHP to export html table layout into excel format (.xls), well the html format opens up fine in Excel but distorted in OpenOffice..
do i have to add any special config to make html format work in OpenOffice?
Any suggesion would be helpful..
I use the below code
header("Content-Type: application/ms-excel");
header("Content-Disposition: attachment;filename=invoice.xls");

It's my practice to create HTML tables and save them as files with names like something.xls
Then both Excel and OpenOffice will open them as spreadsheets.
I don't use PHP, I use Perl, but no Content headers are involved.

Related

Exporting php to excel, text-aligment not working

Hi guys i have been experiencing a rare alignment while exporting php table to excel.
I have set text-alignment:left but still not working.
I have also searched for cell alignment but haven't found the solution.
Here is an image of my table without exporting it to vnd-ms-excel:
And while exporting this happens:
I don't know why the excel file has this alignment, i will add my code but is not pretty clean so i will show the header:
// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");
// Defines the name of the export file "codelution-export.xls"
header("Content-Disposition: attachment; filename=horarios-$fecha.xls");
And this is the part of my code where I add style to my table:
echo '<style>table {width:595px;text-align:left;}
table{border-collapse: collapse;}table,th,td{border:1px solid #000;}</style>';
As you can see in the second image some text is aligned left, other right and other centered.
You can set alignments using PHPExcel library.
these two URLs may help you.
PHPExcel how to apply alignment for the whole document created from mysql table
PHP Excel - Set cell or Column Direction (RTL)

Export data in .xls format rather than tab delimited

HI i am exporting some data from database in .xls format but when i try to save my file it alerts me that file type is not .xls format. I need to store it in excel 97-2003 Workbook. when i pretend for save as option, default file type is in as tab delimited.. how can i overcome it?? below is my code..
if ( $_POST["frmDownload"] == "Excel") {
header("Content-disposition: attachment; filename=employee_details.xls");
header("Content-Type: application/vnd.ms-excel");
header("Content-type: application/x-msexcel");
}
use phpExcel which is excellent for all excel manipulation.
Here is the list of examples.
use the method fromArray to write excel from array which is fetched from persistent data.
use the method toArray to form a php array from excel sheet.
The learning curve is very simple first try first example only from official site.
Then try the fromArray and toArray its very easy and also phpExcel have lots of functionality like gradient background for a cell, border size increasing, formula manipulation and new worksheet also easy to create.
Important: read documentation on when ever you get free time
The format is tab delimited... You are just 'naming' it excel in this way... Look at gvgvgvijayan's answer for the solution to your problem.

I wanted to add data in pre formated Excel sheet using php

I wanted to append data in the pre formated excel sheet that is basically header footer in the excel sheet I wanted to append the contents. And will create many files dynamically.
A simple workaround is:
create a html table with the formatting you need
add values in php to the table (or generate table with php)
save file as .xls (filled with content from html table)
open file (will show formatted table in Excel)
Reason:
handling XLS files is very complex and many libraries have big limits (only available on windows servers....)
html table saved as .xls can be opened in Excel.
Thanks I have found the way PHPExcel is a good library.
In order to get PHPExcel http://www.codeplex.com/PHPExcel working with CodeIgniter, there are a few steps you must take to ensure compatibility with CodeIgniter's naming standards.
1: Class names must match the file names. PHPExcel has a few files(such as PHPExcel/IOFactory.php) that have names like PHPExcel_IOFactory. Change these names by removing the "PHPExcel_" part. These constructors in these files must be public in order for CI to access them.
$this->load->library('phpexcel');
$this->load->library('PHPExcel/iofactory');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("title")
->setDescription("description");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'cell value here');
// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save("nameoffile.xls");

Codeigniter / PHPexcel Set arabic title in properties of xls file

I am working on my web project using Codeigniter. I want to generate an excel file by setting the data in the file, for now that's cool.
but I have a problem is that the site uses Arabic as a primary language, and I would set the title and description in the properties of excel file.
when I open the excel file and I open the properties, it does not display the title and description in Arabic, but like this:
هل تعتقد أن النظام السوري سيستجيب لشروط خاطÙÂي نساء وأطÙÂال من العلوين؟
I tried utf8_decode function but it does not work
thank you
the problem is that the excel file is saved version 5, then I should Register with the 2007 version (. xlsx) and the Arabic values ​​appear correctly in file properities:
$obj_writer = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
$obj_writer->save('php://output');

Force download html to pdf using php

I try to make force download html-file to pdf and use the code below
<?php
header('Content-disposition: attachment; filename=\''. basename($file) .'\'.pdf');
header("Content-type: application/pdf");
echo file_get_contents($_SERVER['DOCUMENT_ROOT'].'/index-1.html');
?>
I get some file with .pdf extention but when I try to open it, i get an error `not PDF or corrupted'. What's wrong and how to make it work?
Telling the browser that some data is a PDF will not magically transform the data into a PDF.
You'll need to do that yourself, e.g. using the PHP PDF library or Prince.
You have to create a properly PDF file. You can do this using some PHP classes like HTML2PDF or wkhtmltopdf.
You can't just put html in a pet file and expect it to work you need to use something like dompdf to convert html to a pdf file: https://code.google.com/p/dompdf/
For even better results and support for floats I would use the following library. I got extremely good results with it. The reason why this one is working so well is because they use the WebKit browser rendering engine to render the html before converting it to pdf giving you almost 1 on 1 results when comparing it to the browser.
https://code.google.com/p/wkhtmltopdf/

Categories