phpexcel - using it with excel template and chart goes missing php - php

Can anyone let me know if there are any other library choices available for me where I can do what I wanted.. that is open the excel template file (.xlsx or even .xlsm) in php and populate the cells and keep all the charts, pivot table, even macros, forms, buttons, etc. intact. All the php will do is just populate the cells with new data from database. In other words, using the same terminology that you have used.. please point me to the library for which I can use to "edit" workbook files.
When i try the below code.. the chart itself goes missing from the resulting file. After trying various ways.. still failed.
Thank you so much again.
<?php
date_default_timezone_set('Europe/London');
require_once 'excel/PHPExcel/IOFactory.php';
require_once 'excel/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load('WorkingFile_Daily.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('B11', 4)
->setCellValue('B12', 5)
->setCellValue('B13', 6)
->setCellValue('B14', 7);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('WorkingFile_Daily New.xlsx');
?>

Related

Copy charts from (.xls or .xlsx) using PHP

I am working on a template excel file for some projects and my goal is to populate it with PHP. I create an excel document with two sheets, one is a table with all of the information that I am going to plot, and the second sheet is just a graph that is based on the table from the previous sheet.
Whenever I use (https://github.com/PHPOffice/PhpSpreadsheet) PHPSpreadsheet (since PHPExcel is deprecated) I am able to read/write/copy/etc the first sheet, but whenever I try to save the sheet without touching the graph sheet at all, nothing happens. I save the file successfully but when I try to open it I get an error stating that Excel found unreadable content in the file and to repair it. I repair it and the second sheet with the graph is empty.
https://github.com/PHPOffice/PhpSpreadsheet/issues/382
This is the closest thing I found to my issue, and I did add the setIncludeChart functions, but nothing seems to work.
I also feel that I have exhausted online searches and am now hoping that there is an alternative to PHPSpreadsheet that will allow me to use a chart in a template (supplied) excel file.
$xls = 'KPI_ReadinessTemplate.xls';
$xlsxTarget = 'NEWX_KPI_ReadinessTemplate.xlsx';
$xlsTarget = 'NEW_KPI_ReadinessTemplate.xls';
$inputFileType = 'Xlsx';
$inputFileName = $xlsx;
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$reader->setIncludeCharts(true);
$spreadsheet = $reader->load($inputFileName);
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getCell('A1')->setValue('Help me');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->setIncludeCharts(true);
$fileData = $writer->save($xlsxTarget);
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$reader->setIncludeCharts(TRUE);
$workbook = $reader->load($xls);
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($workbook);
$writer->setIncludeCharts(TRUE);
$writer->save($xlsTarget);
I am expecting the chart to be copied, in the code from above there is no modification of the file, it should be an exact one-to-one copy of the supplied file but it isn't working at all as it seems the graph gets corrupted and I have to repair the excel the next time I open it.
EDIT: I also installed the archived PHPExcel and tried it using Graph disapear read and write excel file using PHPExcel and I still had the same issue.

Load a Excel file with images using PHPExcel

I'm currently working on a project that needs to display excel files (xls, xlsx, csv) on the browser. So far, I have tried and used the PHPExcel library and was able to display the excel file (code below)
$opendoc = $userDoc;
$objReader = new PHPExcel_Reader_Excel2007();
$objPHPExcel = $objReader->load($opendoc);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->writeAllSheets();
$objw->save('php://output');
The problem I'm encountering is that this code does not support the displaying images (charts , graph , etc) inside an excel file. Any ideas? Thanks in advance!
Errr..... yes it does. Have you read the documentation or looked at the examples? Images are supported directly, and (unless you tell PHPExcel to load data only) should always be loaded.
For charts and graphs, you specifically have to tell PHPExcel to load them when reading a file, and tell PHPExcel to save them when Writing. (Example)
$opendoc = $userDoc;
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setIncludeCharts(TRUE);
$objPHPExcel = $objReader->load($opendoc);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objw = $objWriter;
$objw->setIncludeCharts(TRUE);
$objw->writeAllSheets();
$objw->save('php://output');

PHPexcel How to show multiple sheets data in browser

<?php
require_once("PHPExcel/Classes/PHPExcel.php");
require_once("PHPExcel/Classes/PHPExcel/Writer/Excel2007.php");
require_once("PHPExcel/Classes/PHPExcel/IOFactory.php");
$objPHPExcel = new PHPExcel();
$inputFileName = 'R1.xlsx';
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objPHPExcel->setActiveSheetIndex(0);
$objWriter->save('php://output');
This is my code its good to get excel Sheet1 output from R1.xlsx file to browser. But R1.xlsx contain more than one sheets how to show them by link or vertically to show sheet2 and sheet3 data?
Thanks
PS.
I tried
$objPHPExcel->setActiveSheetIndex(X); by changing X value
By default, the HTML Writer will only generate output for a single worksheet
You can specify which sheet to write by calling
$objWriter->setSheetIndex(2);
specifying the individual sheet that you want to output
But you can also tell it to generate an output for all sheets instead using
$objWriter->writeAllSheets();
before the save
You can use a for loop, in each loop, output the content of each table.

PHPExcel removes chart style when template is loaded

I am using PHPExcel to read and write values to Excel. I have a template that has a graph and a style on it.
Here's the issue:
After I appended some value on the Excel sheet, the style of the chart has been remove and it shows the default style.
I am using this code to read:
$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("rcca.xlsx");
And this to write
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('rcca.xlsx');
Am I missing something?
Please see the screenshot of the chart style before and after:
Had the same problems with charts using PHPExcel
(my main problem was to change colors of chart bars)
First of all if some one did not know or forgot xlsx files are zip archs
So the main problem is that PHPExcel when saving xlsx file adds default theme file xl/theme/theme1.xml even if you change it at $objReader->load file it will overwrite it to default at $objWriter->save.
Charts colors in that file theme1.xml at <a:accent1> (...2,3,4,5,6)
so i took theme1.xml changed colors to which i need, saved and using The ZipArchive class overwrite it after
$objWriter->save with simple code
$zip = new ZipArchive;
if ($zip->open('filename.xlsx') === TRUE) {
$zip->addFile('mytheme1.xml', 'xl/theme/theme1.xml');
$zip->close();
echo 'ok';
} else {
echo 'err';
}
I hope it will help someone
It seems that chart styling need to be hard-coded at this time.
On this other thread someone explain how to add some styling PHPExcel graph design (border, graph color, graph inner position)

PHP Excel Output <inputs> in html

I'm currently using PHP Excel to output an excel file in html. I was wondering if it was possible to edit the source code so that instead of outputting a table, it outputs an input or textarea instead. I'm not quite sure where to begin. I'm trying to take a look at all the classes in PHPExcel and can't find where the tables are being outputted so I can change them. If anyone can help me that would be great! Thanks
$inputFileName = "file";
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->writeAllSheets();
$objWriter->save('php://output');
Apparently it's written in /Classes/PHPExcel/Writer/HTML.php.
Maybe you could create your own writer, by extending PHPExcel_Writer_Abstract and implementing the interface IWriter?
I think it would be simpler to create your own method to be able to add all your specific needs (image, layout, etc ..). And then share it ;)

Categories