PHPExcel - saving images while modify spreadsheet - php

I'm reading spreadsheet and change values of some cells, then save it with
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel2007');
$objWriter->save('write.xls');
But it doesn't save any images.
How to transfer images from original file to new?
We can get images $objPHPExcel->getActiveSheet()->getDrawingCollection() but how put this into new file?

Found out the reason of lost images. They have been copied to excel from .doc directly, when I paste images to excel through menu they've been saved in output file.

Related

PHPExcel saving xlsx to xls, some cells are blank

I have code for converting xls to xlsx via PHPExcel:
$objPHPexcel = PHPExcel_IOFactory::load('file.xlsx');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
$objWriter->save('new_file.xls');
But when I open created xls file some cells are empty.
What could be the problem?
Thanks!
Link to download original xlsx file
you should use one of following as formate type.
1)Excel5 -> file format between Excel Version 95 to 2003
2)Excel2003XML -> file format for Excel 2003
3)Excel2007 -> file format for Excel 2007
and add following line at line no.3 in your code before save file statement.
$fileType=PHPExcel_IOFactory::identify("file.xlsx"); //We can get file reader type automatically using
it generate file as per your requirement.
I do notice that some of these cells contain references to external files, e.g. cell A23 contains
='[Форма 1-2 от 15.12 ЯНВАРЬ екат.xls]НТТЗМ'!A77
Logically, PHPExcel can't simply access that external file (Форма 1-2 от 15.12 ЯНВАРЬ екат.xls) to retrieve data; nor can it handle references to external files... the file may not even exist; and if it did, there is a major overhead in loading (recursively) every external file that might be referenced in formulae.
While you haven't indicated which cells might be blank in the newly saved file, it would be my guess that they're the cells that contain these external file references

Loading macros from .XLS file

I have an .XLS file with macros I have to modify and keep the macros and images it contains. I can open the file and modify the cell data but it loses the macros and images. I tried to search for the problem on Google but I only found solutions for .xlsm files. I hope it's possible to keep the macros in an .xls file too. Here is my code for the opening and saving:
include_once libraries_get_path('PHPExcel').'/PHPExcel.php';
include_once libraries_get_path('PHPExcel').'/PHPExcel/IOFactory.php';
$template = PHPExcel_IOFactory::load('private://xls/form_template.xls');
$template->setActiveSheetIndex(0);
// Modifying the cells data here...
$objWriter = PHPExcel_IOFactory::createWriter($template, 'Excel5');
$objWriter->save('sites/default/files/'.$filename);
It's a really important function in the project and the customer needs it ASAP.

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)

PHPExcel file not in the proper Microsoft Excel 2003 XLS format

I'm generating a Excell 2003 using PHPExcel 1.8 with this code:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('toUploadFtp.xls');
The file seems ok, and I can read it using MS Office and LibreOffice.
But I have to upload the file to a system that says the format has to be Excell 2003, and he prompts next error:
Oops! Your file is not in the proper Microsoft Excel 2003 XLS format.
If I open the file with LibreOffice and save it again, then I can upload the file correctly to the system.
Any idea?
Thanks,
Create your file by passing the proper version to factory:
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
Hope it helps.
Not understanding the exact problem entirely: it seems to be with the additional transfer of the Excel file.
One error is often, that the PHP script outputs spurious data.
Leaving out the final %> is a standard trick to make sure no extra final new line characters are added to the file.
Comparing the files generated and transferred should yield insight on what went wrong.
You may need to set the content to non-text to prevent some text conversion:
header('Content-Type: application/octet'):
Finally I couldn't find a solution. It has to be some kind of bug in the XLS 2003 format.
But I generated a XLSX with $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); and the system recognizes it correctly.
I think you can overcome this problem in changing the filed of Excel5.Since i havnt tried phpexcel i dont know much about this..But i think you can find a solution for this [here].1

PHPExcel create PDF

I am having a problem creating a PDF using PHPExcel. I can create the excel and pdf file fine, but its the characters on the pdf that isn't working properly. Image below.
If I copy any of the text and paste it somewhere it reads as normal text and not gibberish anymore, which is very strange. My code is below.
$newpdf = "reports/risk-bordereaux-".$time.".pdf";
$objPHPExcel = PHPExcel_IOFactory::load($newxlsx);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToWidth(true);
$objPHPExcel->getActiveSheet(0)->getPageSetup()->setFitToHeight(true);
$objPHPExcel->getActiveSheet(0)->setShowGridlines(false);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save($newpdf);
I'm wondering is there any way I could set the character encoding or something to stop this happening? Any help would be greatly appreciated!
EDIT
Link to bigger image
I am using TCPDF to generate PDF's

Categories