Problem with PHPSpreadsheet. When I read my own template file and opened the saved one, the template style was applied only to the first row or column, and the rest of the cells became the default style. I want the template style to be applied to all cells.
The executed code is as follows.
$spreadsheet = IOFactory::load('sample.xlsx');
$sheet = $spreadsheet->getActiveSheet();
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('write.xlsx');
exit();
Maybe not a code issue, but a spreadsheet issue. If you have a similar problem, I'd like to give some advice so I can do it a little.
Thank you.
The styles will not automatically taken from the template. You can set them manually.
Take a look to Documentation.
Not all kind of styles are implemented. Pivot-Tables for example aren't supported. If you use format as table you use an pivot-table and yes this is not supported as well.
Related
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;
?>
So I'm using PHPExcel to generate spreadsheets with data from a database. I have specific columns set to specific widths. When ever I try to use a default font for the entire spreadsheet though, these widths seem to be ignored and the sheet widens. Am i using this wrong? Where is #Mark Baker?
$objPHPExcel = new PHPExcel();
$phpColor = new PHPExcel_Style_Color();
$phpColor->setRGB('051C48');
$objPHPExcel->getDefaultStyle()->getFont()->setColor($phpColor);
$objPHPExcel->getDefaultStyle()->getFont()->setName('Gotham Book');
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
$activeSheet = $objPHPExcel->getActiveSheet();
$activeSheet->setShowGridlines(false);
foreach(range('A','F') as $columnID){
$objPHPExcel->getActiveSheet()->getColumnDimension($columnID)->setAutoSize(false);
}
///////////////////////////////// FIRST PHASE /////////////////////////////////////////////////////////
$activeSheet->getColumnDimension('A')->setWidth(22.71);
$activeSheet->getColumnDimension('B')->setWidth(31.71);
$activeSheet->getColumnDimension('C')->setWidth(9.71);
$activeSheet->getColumnDimension('D')->setWidth(9.71);
$activeSheet->getColumnDimension('E')->setWidth(11.85);
$activeSheet->getColumnDimension('F')->setWidth(17.57);
NO DEFAULT FONT SET:
DEFAULT FONT SET:
If you would like any additional information, just let me know. I realize those screenshots may not be the best method for showing whats happening. I can however just set a range for the cells, I was just hoping for cleaner code by setting a default value. Also if I'm being to specific and needy, just ignore me
$activeSheet->getStyle('A1:F1')->applyFromArray($firstRowStyle);
$activeSheet->getStyle('B2:B50')->applyFromArray($gothamBook10);
$activeSheet->getStyle('C2:C50')->applyFromArray($gothamBook10);
$activeSheet->getStyle('D2:D50')->applyFromArray($gothamBook10);
$activeSheet->getStyle('E2:E50')->applyFromArray($gothamBook10);
$activeSheet->getStyle('F2:F50')->applyFromArray($gothamBook10);
I am trying to use the PHPExcel class to export reports into excel file. What I really need to do is to go thru every row in the HTML table then place it into cells in an excel file.
I think PHPExcel has enough document to help me with created the excel file. But what I am not sure on how to do is to take an html table and break it so I can take every value and put it in an excel cell.
Thanks for your help
As simple as:
$objReader = PHPExcel_IOFactory::createReader('HTML');
$objPHPExcel = $objReader->load("myHtmlFile.html");
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save("myExcelFile.xlsx");
but you could use your same basic logic for building the HTML from your report data, and just modify it to set cell values in a PHPExcel object. If you have an MVC, then its only the View that you really need to change.
If you want to mess with HTML but in PHP, i recommend SimpleHTMLDom
If you like jquery, you'll love it.
Example code:
$html = str_get_html('<table id="mytable"><td>cell ??</td><td>cell 2</td></table>');
$html->find("#mytable td:first-child",0)->innertext = "cell 1!";
echo $html;
done.
You loop through it with jQuery
$('td').each(function(){
//do stuff here...
});
jQuery API docs for .each
I have tried to use phpexcel with my own template file. phpexcel loads the file and writes data to some cells A2, A3, A4 for example.. and opens an output file with the new data.
my template file has chart built-in.. all i want to phpexcel to do is to populate values in cells and don't touch the chart. And, open the new file. (Please note that I don't want to make the chart in code.. I want the chart to pre-exist with in my template in same format as I created originally). Only the data should update.
But, when i try to do this.. the chart itself goes missing from the resulting file. After trying various ways.. still failed.
And, i found the following code from http://phpexcel.codeplex.com/discussions/397263
require_once 'Classes/PHPExcel.php';
/** PHPExcel_IOFactory */
include 'Classes/PHPExcel/IOFactory.php';
$target ='Results/';
$fileType = 'Excel2007';
$InputFileName = $target.'Result.xlsx';
$OutputFileName = $target . '_Result.xlsx';
//Read the file (including chart template)
$objReader = PHPExcel_IOFactory::createReader($fileType);
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($InputFileName);
//Change the file
$objPHPExcel->setActiveSheetIndex(0)
// Add data
->setCellValue('C3','10' )
->setCellValue('C4','20' )
->setCellValue('C5','30')
->setCellValue('C5','40' );
//Write the file (including chart)
PHPExcel_Settings::setZipClass(PHPExcel_Settings::PCLZIP);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
$objWriter->setIncludeCharts(TRUE);
$objWriter->save($OutputFileName);
The above code works in excel 2010 and now keeps my chart in tact... but still when I try to use filetype "Excel5" it doesn't work.
It throws the following error:
Fatal error: Call to undefined method PHPExcel_Reader_Excel5::setIncludeCharts()
in D:\IT\bfstools\PHPExcel\MyExamples\test1.php on line 16
Please provide a simple solution where I want my template file to work with .xls and .xlsx and all my original chart in the template file should stay intact. I do not want the chart removed it from the resulting file. Neither do I plan to create the chart using phpexcel code. (why write unnecessary code when excel can do all the work for you).
I want the easiest way out which is just to use everything with in my template and just populate cells with new data. And, my existing chart in the template comes live automatically. I don't want to write unnecessary code while I can safely rely on excel template and charting functions.
Please help.
There's a very good reason for this:
Charting is only implemented in core, and for the Excel2007 Readers and Writers at this point in time, so all of the other readers or writers will ignore charts, treat them as though they simply don't exist. The intention is to roll out charting to the other readers/writers over the coming year.
EDIT
I see from your comment that you don't understand how PHPExcel works at all, so I have a lot of explaining to do.
PHPExcel is not a library for "editing" workbook files: you're not using PHPExcel to change a file, you're changing a PHPExcel object that can be loaded from a file, and can subsequently be written to a file.
PHPExcel Core is an in-memory representation of the spreadsheet, with the different constituent objects such as worksheets, cells, images, styles, etc all represented as PHP Objects.
The PHPExcel Readers parse a spreadsheet file and load all the components from a file that they have been programmed to recognise, and create the appropriate PHPExcel core objects from those file components. If there is no equivalent PHPExcel Core object (such as Pivot Tables), then that file component can't be "loaded"; if the loader hasn't been programmed to recognise a file component, then it can't be loaded. In these cases, those elements from the file are simply ignored. Once the Reader has done it's job, a PHPExcel object exists, and the spreadsheet file is closed and forgotten.
When a PHPExcel Core object exists in memory, you have a set of methods allowing you to manipulate and change it, to add, modify or delete Core elements; but these work purely on the "in memory" collection of worksheet, cell, style objects that comprise the PHPExcel Core. The Core exists without knowledge of having been loaded from a file or having been created using a PHP "new PHPExcel()" statement; it makes no changes to files in any way.
When writing, the reverse is true. Each Writer takes the PHPExcel core objects, and writes them to a file in the appropriate format (Excel BIFF, OfficeOpenXML, HTML, etc). Like the Readers, each writer can only write those PHPExcel Core objects that it has been programmed to write. If it has not been programmed to write (for example, charts) then any charts defined in the PHPExcel Core will be ignored because that writer simply doesn't know how to write them yet. Likewise, features that exist in PHPExcel Core that are not supported by the file format that is being written to (such as cell styles for the CSV Writer) are ignored.
So to support a spreadsheet feature such as charts, it is necessary for the PHPExcel Core object collection to have been modified to provide an "in memory" representation of those elements, and for the different Readers to have been programmed to recognise those elements in the file they are loading and to convert them to the appropriate PHPExcel Core objects, and for the different Writers to have been programmed to convert the PHPExcel core representation to the appropriate file representation.
Each Reader and each Writer needs to be programmed individually. Charts is a relatively new feature, only added to the PHPExcel Core in the 1.7.7 release, and at this point only the Reader and Writer for the Excel2007 format have been programmed to recognise chart elements.
While it is the intention of the developers to extend this to cover the other formats as well, the necessary code isn't created automagically. Programming each individual Reader and Writer takes time and effort. While the Chart code for the Excel2007 Reader and Writer has now stabilised to the point where it is now no longer considered "experimental", and development focus is turning to writing the necessary code for chart handling in the Excel5 Reader and Writer, it is work that has not yet been completed.
If you can use Golang, try Excelize. Support save file without losing original charts of XLSX.
Try set setIncludeCharts
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
// Tell the reader to include charts when it loads a file
$objReader->setIncludeCharts(TRUE);
// Load the file
$objPHPExcel = $objReader->load($filePath);
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);