Load a Excel file with images using PHPExcel - php

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');

Related

PHPExcel - comment background become black in excel

I'm using PHPExcel to load and save *.xslx file in PHP.
I load a normal excel file and save it into another file using PHPExcel then all the comments become black comments.
Do you have any idea what it could be?
Thanks
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($loadfilename);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');//Excel2007
$objWriter->save($savefilename);
Here is the result of excel comment

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.

save php spreadsheet template as PDF using Mpdf

i need some help with phpspreadsheet, i'm trying to load an excel spreadsheet, add something to it and then save it as a pdf, the problem is that when i load a template , saving as pdf doesn't work
ps : when i create a new spreadsheet it works fine.
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load('docs/MODEL_FACTURE_DOM_CRE.xlsx');
$spreadsheet->setActiveSheetIndex(0)
->setCellValue('B13', 'test')
->setCellValue('F13', 'test');
$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->setShowGridLines(false);
$rendererName = Settings::PDF_RENDERER_MPDF;
Settings::setPdfRendererName($rendererName);
$writer = IOFactory::createWriter($spreadsheet, 'Pdf');
$filename = 'FACTURE.pdf';
$writer->save($filename);
Any suggestions ?
Try the older version. On MPDF version v5.7.4a, your code is work.
In more recent versions there are incompatible changes.

Excel 97-2003 file incompatible after PHPExcel reads and writes

I have an excel file that has been created and saved as an Excel 97-2003 file. I am using the following code to read and then write using PHPExcel.
$objReader = new \PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load(storage_path() . "/exports/file.xls");
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Test Message');
$objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter-> save(storage_path() . "/exports/file.xls");
I'm using SamApp ExcelSMS Android app that will then read the file. The only problem is that the file is now incompatible when overwritten with new content. Also, the file size decreases from 18KB to 5KB.
Any help on this is greatly appreciated.

PHPExcel to html gives empty cells

Im Using the PHPExcel library to convert a uploaded .xlsx file to a html page.
The code works but the html page also has alot of empty cells in it. what can i do so PHPExcel wil not create these?
the convert
$inputFileName = $upload_dir;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'HTML');
$objWriter->save($basename.".html");
the result

Categories