Excel / PHPSpreadSheet : LOAD and SAVE WITH Charts, unexpected argument - php

I'm trying to load my excel file through PHPSpreadSheet library including the charts.
At the reader step, everything is working fine and I could without difficulty use the LOAD_WITH_CHARTS flag as the documentation.
But when I'm trying to save my file (I have to save it with the charts as well), fatal error.
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Calculation\Exception: Formula Error: Unexpected ,
PHPSpreadSheet doesn't seem to accept my second argument after the , (the flag).
Here my PHP code directly took from the documentation :
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
$reader = IOFactory::createReaderForFile("books.xlsx");
$spreadsheet = $reader->load("books.xlsx", $reader::LOAD_WITH_CHARTS);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'New Value');
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('test.xlsx', $writer::SAVE_WITH_CHARTS);
I tried this :
$writer->save('test.xlsx');
It is working fine without the SAVE FLAGS, it seems to be the problem but I really need this flag.
Here the doc part about flags on PHPSPreadSheet https://phpspreadsheet.readthedocs.io/en/latest/topics/reading-and-writing-to-file/#readerwriter-flags
Any idea ?
Thanks for reading, have a nice end of week
EDIT :
By adding
$writer->setPreCalculateFormulas(false);
It seems to be working, but by adding this parameter the file loose a lot of settings like the Charts color by example

Related

Reading File With PhpSpreadsheet Sheet Out of Bounds Error

Trying to read from a XLSX file with PhpSpreadsheet however getting below error:
PHP Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Exception: Your requested sheet index: -1 is out of bounds. The actual number of sheets is 0. in C:\php\projects\stock\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Spreadsheet.php:678
There is definitely a worksheet and my code below to pull a single cell works fine when I remove the table and just have unformatted data.
$spreadsheet = new PhpOffice\PhpSpreadsheet\Spreadsheet();
$inputFileName = "path/to/temp_file_download.xlsx";
$inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
$spreadsheet = $reader->load($inputFileName);
echo $cellValue = $spreadsheet->getActiveSheet()->getCellByColumnAndRow(2, 2)->getValue();
It seems that saving the file with Excel allows it to work just fine however excel does not complain that it is corrupt etc. I have no control over the actual spreadsheet as generated from an external source.
Just wondering if anyone has had similar issues and have any ideas for working around or what I may be doing wrong?
Tried to use the older PhpExcel also to get this to work however similar issues occur until I save the file again.
Have also tried XLSXReader with similar issues:
PHP Fatal error: Uncaught Exception: File /xl/workbook.xml does not exist in the Excel file in C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php:57
Stack trace:
#0 C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php(70): XLSXReader->getEntryData()
#1 C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php(47): XLSXReader->parse()
#2 C:\php\projects\stock\stock.php(17): XLSXReader->__construct()
#3 {main}
thrown in C:\php\projects\stock\vendor\XLSXReader\XLSXReader.php on line 57
And have seen many issues on web for people having issues reading these files that area created like so however still not sure of a simple workaround.
Adding the workaround mentioned below hack for non standard xlsx seemed to do the trick!
https://github.com/PHPOffice/PHPExcel/issues/1187
Just need to add the class extending the xlsx class to phpspreadsheet then call that class instead and all works!

PhpSpreadsheet cannot read specific xls file

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.

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.

How to display an Excel with PhpSpreadsheet when loading the information with PHP

I am currently using the PHPSpreadsheet library to take values from PHP to Excel, what it does now is to take the values to an excel and store them in the address that I specify. but what I'm looking for is that when I upload information, show me the excel to open. When I used the PHPExcel if I could, and it was done with: $ objWriter-> save ('php: // output') then the excel was displayed to open. My code with PHPSpreadsheet is the following:
<?php
require_once("vendor/autoload.php");
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$a="we";
$a1="are";
$a2="the";
$a3="world";
$sheet->setCellValue('A2', $a);
$sheet->setCellValue('c2', $a1);
$sheet->setCellValue('E2', $a2);
$sheet->setCellValue('G2', $a3);
$writer = new Xlsx($spreadsheet);
$writer->save('C:\xampp\example.xlsx');
?>
Could it be done?
You can't show a Excel file without an application for open it, I think you could use another web application for display the file on web browser.

Yii - how to read excel file sheets using yexcel plugin

I am using Yexcel - YII plugin that is a wrapper for PHPEXcel for a web application that need to read and parse different excel files.
http://www.yiiframework.com/extension/yexcel/
I am able to read and use the data in excel files that are single means they have only
one sheet but when it comes to reading other sheets in same excel file then i cannot read those because it gives me error.Using PHPExcel we can set active sheets and read
like this
$objPHPExcel->setActiveSheetIndex(0); // first sheet
but how can we do this using YII's yexcel plugin?
Because when i call this function with yexcel object it gives me error
Fatal error: Call to undefined method Yexcel::setActiveSheetIndex()
Issue: How to access all sheets in an Excel file.
Any help, guidance in right direction is much appreciated.
Use the import csv extension
yii import csv
You can either extend Yexcel class and write your own method based on Yexcel->readActiveSheet() or hardcode it (bad practice).
Below is my modified version:
public function readActiveSheet( $file, $activeSheet = 0 )
{
$objPHPExcel = PHPExcel_IOFactory::load($file);
$objPHPExcel->setActiveSheetIndex($activeSheet);
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null, true, true, true);
return $sheetData;
}
You can use it as following:
$sheet_attay = Yii::app()->yexcel->readActiveSheet($filename, 1);

Categories