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.
Related
I have a template excel sheet saved as template.xls.I'm trying to get it's copy of the template sheet into another workbook. below is my code ,
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::
load('report/dailyReport_Line'.$_REQUEST['line'].'_'.$year.'_'.$month.'.xls');
$template = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xls');
$clonedWorksheet = clone $template->getSheetByName('template');
$clonedWorksheet->setTitle($date);
$spreadsheet->addSheet($clonedWorksheet);
when i go to the corresponding $spreadsheet file,the copied excel file is not updated. I'll appreciate if someone could assist me to solve it
The following error
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Reader\Exception: Parameter pos=-12 is invalid
is given when trying to parse a specific xls file.
Code
$inputFileName = "excel.xls";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xls');
$spreadsheet = $reader->load($inputFileName);
The file in question: https://filebin.net/sle19tm0kdgduyne/excel.xls?t=u0itbeue
I have tried using all available readers such as Xlsx, Csv etc and even using the old deprecated PHPExcel library. Nothing can parse this specific file, even though it opens fine with excel on windows.
My end goal is converting this xls file to an array, so i can paste the data into a database.
I think you don't need to use createReader().
Here the example of my worked code.
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
$spreadsheet = $reader->load('path/to/file.xls');
Hope this can help you.
I am trying to read excel file in my CodeIgniter application. The function getActiveSheet()->toArray(null,true,true,true); is working fine for an excel file with 14442 x 17 cells, however this function does not works for an excel file with 17590 x 17 cells. In this second case, browser ends-up with a blank page and I am not getting any error. So please tell what can be the issue?
Code:
$objPHPExcel = PHPExcel_IOFactory::load($file_path);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
Probably out of memory. It is a common issue with large excel files.
If you only need to read the data you can use something like
$objReader = PHPExcel_IOFactory::createReaderForFile($file);
$objReader->setReadDataOnly(true);
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');
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)