Exporting php to excel, text-aligment not working - php

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)

Related

PHP: Create an excel table

I need to create an excel table with help of php. Is that possible? The php code below creates and write to an excel file. I would like to create an excel table with the data, see picture below. I'm using PhpSpreadsheet (sorry I forgot to say that)
Edit: The code below uses library PhpSpreadsheet to ceate a excel file with some content.
I want to create an excel table:
The code works and create the content as the second picture below shows. This is NOT an excel table, just plain text in cells.
But that is not what I want. I want to be able to create the content as the first picture below shows. This is an excel table. When you create an excel table by hand you can choose colrs etc. I do not care about the colors. Excel add the column name and push the content down.
What I have tried is to add: $sheet->setAutoFilter('A1:B5'); to the code, but this does not create an excel table as shown in the third picture below.
So the question is: What do I need to add to the code above to be able to create the content as shown in the first picture below
The fourth picture below shows how to crate an excel table in excel (and this is what I want the php code to do)
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'A1');
$sheet->setCellValue('A2', 'A2');
$sheet->setCellValue('A3', 'A3');
$sheet->setCellValue('A4', 'A4');
$sheet->setCellValue('A5', 'A5');
//$sheet->setCellValue('A6', 'A6');
$sheet->setCellValue('B1', 'B1');
$sheet->setCellValue('B2', 'B2');
$sheet->setCellValue('B3', 'B3');
$sheet->setCellValue('B4', 'B4');
$sheet->setCellValue('B5', 'B5');
$writer = new Xlsx($spreadsheet);
$writer->save('CreateExcelTable.xlsx');
The picture below show the tableI would like to create
With the code above this is created:
With the code added:
$sheet->setAutoFilter('A1:B5');
The picture below show what is created.It is not a table
Insert/Table is simply a GUI "shortcut" method for styling and setting autofilters against a block of cells. Both of these can be done as individual tasks using PHPSpreadSheet, but the library does not provide a "shortcut" way of doing this with a single method call.
Take a look at section of the Developer documentation.
I have a similar problem.
My Excel Template had some array formulas that refer to Named Data Tables,
(instead of writing in the array formula A2:A150 i would simply write NameOfMyTable[NameOfColumn])
When i inserted some data in my template using PhpSpreadSheet and downloaded the produced excel file, Each instance of NameOfMyTable[NameOfColumn] was replaced with REF!
Instead of searching how make or preserve Named Data Tables while using PHPSpreasheet. I simply converted all my Named tables back to simple ranges. (you can easily do this conversion through TableTools in excel without having to change every formula in your Excel Template).
I have a code I wrote, use it if you want
look at the code
using
$excelExport = new ExcelExport(['name' => 'dılo'], 'file');
$excelExport->download();
shortest excel creation example with php
basic example
<?php
$file="demo.xls";
$test="<table ><tr><td>Cell 1</td><td>Cell 2</td></tr></table>";
header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=$file");
echo $test;
?>

Issues with exporting html to excel

For exporting our reports, we set the header to 'Content-Disposition: attachment;filename=fileName.xls' and dump the html to the output buffer and it downloaded the excel file nice and clean.
This was used to work until recently and now the downloaded file does not open in Excel. It doesn't give any error, just shows the blank gray screen similar to the screen when you close all the worksheets.
Strange enough, if you just open the file in Notepad++, add a space and save it, Excel WILL open it.
I suspect it to be something to do w/ the encoding of the file which is not acceptable to excel when the file is downloaded but something changes after you edit it in Notepad++ and excel would open it after the edit.
I have tried using ob_clean(), trim and converted the content to utf8 before dumping but nothing seems to work, what am I doing wrong?
Here is a sample code that recreates the issue I have:
ob_clean();
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=filename.xls");
header('Cache-Control: max-age=0');
header_remove('Pragma');
// simple html table
echo '<table><thead><tr><td>header 1</td><td>header 2</td></tr></thead><tbody><tr><td>content 1</td><td>content 2</td></tr></tbody></table>';
The above code snippet downloads the file 'filename.xls', excel initially does not open it, but after editing it the way I described earlier, excel WILL open it.
Thanks.
I had the exact same problem, then searching i found this!
"The Excel team has made a change in the behavior of certain file types to increase security." On: https://support.microsoft.com/en-us/kb/3181507
You can try one of the possible Solutions/Workaround.
The problem is the exel is blocking the unsafe document which downloads from internet.
To avoid this, you have change some settings in excel.
Options -> Trust Center -> Trust Center Settings -> Protacted View
Untick the Enable Protected View for files originated from the internet.
Save it
That's it.
You can open the files.

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.

PHPExcel PDF Layout

So I am exporting an Excel file I created with PHPExcel to PDF. My only problem is when the content runs over the width of the page (not the length) that content disappears. Only a blank page is created where that content should be. I've tried inserting a column break with a line like this example from the documentation, but only row breaks seem to work with PDF:
$objPHPExcel->getActiveSheet()->setBreak( 'D10' , \PHPExcel_Worksheet::BREAK_COLUMN );
It's worth mentioning I'm working from within a Symfony2 controller, hence the slash in front of the PHPExcel enum.
My ideal solution is to have any extra content run over to a second page that can be placed alongside the first page to show the entire table as it appears in a real Excel document.
This is a limitation of the external library used by PHPExcel to render html output as PDF (namely tcPDF).
While I'm aware of a number of other libraries that can generate PDF from HTML (such as domPdf and mPdf) and future versions of PHPExcel will allow you to select which of these libraries you wish to use, none of them seem capable of generating multiple pages to show data from the full width of a worksheet.
Unless anybody can suggest alternatives, I'm afraid that this will remain a limitation for the foreseeable future.
I was having a similar problem vertically,
the solution i found was to use this
$highestRow = $objPHPExcel->setActiveSheetIndex(0)
->getHighestRow();
$objPHPExcel->setActiveSheetIndex(0)
->getPageSetup()
->setPrintArea("A1:I" . $highestRow )
->setFitToHeight(255);
i would suggest trying something similar with width.
for example :
$highestColumn= $objPHPExcel->setActiveSheetIndex(0)
->getHighestColumn();
$objPHPExcel->setActiveSheetIndex(0)
->getPageSetup()
->setPrintArea("A1:" . $highestColumn . "10" )
->setFitToWidth(255);

Problem with php exported xls file in Open Office

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.

Categories