I am trying to load excel sheet (generated by other application) using phpexcel library, with following code:
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('callsheet.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$dataArray = $objPHPExcel->getActiveSheet()->toArray(null, true,true,true);
var_dump($dataArray);
But I got this error.
Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0.' in E:\xampp\htdocs\podioexcel\Classes\PHPExcel.php:688 Stack trace: #0 E:\xampp\htdocs\podioexcel\test.php(18): PHPExcel->setActiveSheetIndex(0) #1 {main} thrown in E:\xampp\htdocs\podioexcel\Classes\PHPExcel.php on line 688
One interesting thing. when I open same excel sheet and just save file without any change. Than this code work fine , but problem is that I,m not going to open file each time before to use it. Can you please help me to fix this issue.
Related
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!
I´m working with phpspreadsheet and I want to modify an xlsx file with 4 sheets. I only want to insert data in 2 sheets, but I want to copy all 4 sheets to the new xlsx file. When I do that I get this error:
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried
to allocate 20480 bytes)
This is because one of that sheets is to heavy, but that heavy sheet I only want to copy it, not modify it. I have tried loading this:
ini_set('memory_limit', -1);
But it doesn´t work for me, because it goes out of defined runtime (more than 120 seconds).
I have also try this:
$inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
$sheetnames = ['Data Sheet #1','Data Sheet #3'];
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$reader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
But this only copies to the new file the specified sheets.
EDIT:
I have tried to copy the sheets that I don´t need to edit with the following code:
$spreadsheet1 =\PhpOffice\PhpSpreadsheet\IOFactory::load("./sampleData/example1.xls");
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Data Sheet #2 ');
$clonedWorksheet->setTitle('Test');
$spreadsheet->addSheet($clonedWorksheet);
But now I get another error:
Fatal error: Uncaught Error: Call to a member function getCell() on
null in
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php:2785
Stack trace: #0
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php(262):
PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue(Object(PhpOffice\PhpSpreadsheet\Cell\Cell),
true) #1
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1077):
PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue() #2
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1027):
PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCell(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter),
Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), 'M7', Array) #3
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(76):
PhpOffice in
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php
on line 2785
I think that this is because with clone you can´t copy formulas. Can this be the reason? Is it another solution to copy a sheet with formulas?
Finally I have found the solution. The problem was on the writer and the formulas, so have add this line to the code and now it works well:
$writer->setPreCalculateFormulas(false);
So the all the code for the writer now is:
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save('test.xlsx');
I´m working with phpspreadsheet and I want to modify an xlsx file with 4 sheets. I only want to insert data in 2 sheets, but I want to copy all 4 sheets to the new xlsx file. When I do that I get this error:
Fatal error: Allowed memory size of 536870912 bytes exhausted (tried
to allocate 20480 bytes)
This is because one of that sheets is to heavy, but that heavy sheet I only want to copy it, not modify it. I have tried loading this:
ini_set('memory_limit', -1);
But it doesn´t work for me, because it goes out of defined runtime (more than 120 seconds).
I have also try this:
$inputFileType = 'Xls';
$inputFileName = './sampleData/example1.xls';
$sheetnames = ['Data Sheet #1','Data Sheet #3'];
/** Create a new Reader of the type defined in $inputFileType **/
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
/** Advise the Reader of which WorkSheets we want to load **/
$reader->setLoadSheetsOnly($sheetnames);
/** Load $inputFileName to a Spreadsheet Object **/
$spreadsheet = $reader->load($inputFileName);
But this only copies to the new file the specified sheets.
EDIT:
I have tried to copy the sheets that I don´t need to edit with the following code:
$spreadsheet1 =\PhpOffice\PhpSpreadsheet\IOFactory::load("./sampleData/example1.xls");
$clonedWorksheet = clone $spreadsheet1->getSheetByName('Data Sheet #2 ');
$clonedWorksheet->setTitle('Test');
$spreadsheet->addSheet($clonedWorksheet);
But now I get another error:
Fatal error: Uncaught Error: Call to a member function getCell() on
null in
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php:2785
Stack trace: #0
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Cell\Cell.php(262):
PhpOffice\PhpSpreadsheet\Calculation\Calculation->calculateCellValue(Object(PhpOffice\PhpSpreadsheet\Cell\Cell),
true) #1
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1077):
PhpOffice\PhpSpreadsheet\Cell\Cell->getCalculatedValue() #2
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(1027):
PhpOffice\PhpSpreadsheet\Writer\Xlsx\Worksheet->writeCell(Object(PhpOffice\PhpSpreadsheet\Shared\XMLWriter),
Object(PhpOffice\PhpSpreadsheet\Worksheet\Worksheet), 'M7', Array) #3
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx\Worksheet.php(76):
PhpOffice in
C:\xampp\htdocs\OfferConfigurator\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php
on line 2785
I think that this is because with clone you can´t copy formulas. Can this be the reason? Is it another solution to copy a sheet with formulas?
Finally I have found the solution. The problem was on the writer and the formulas, so have add this line to the code and now it works well:
$writer->setPreCalculateFormulas(false);
So the all the code for the writer now is:
$writer = new Xlsx($spreadsheet);
$writer->setPreCalculateFormulas(false);
$writer->save('test.xlsx');
Use-case:
Export data from different apps using batch EXPORT APIs to merge into a master excel file and use it for reporting purpose...
Implementation Details
I'm using Podio export API to get data from an application. Application name is Kall8-number-text (as an example).. here is the code snippet
Code Snippet
$batch_id = PodioItem::export(11804702,"xlsx",array("filters" => array( "kall8-number-text" => "510-592-5916") ));
PodioBatch::get( $batch_id );
$file = PodioFile::get($file_id);
// Download the file. This might take a while...
$file_content = $file->get_raw();
// Store the file on local disk
$path_to_file= "downloads/".$name;
file_put_contents($path_to_file, $file_content);
Problem Description:
I'm trying to read downloaded file using phpexcel library but getting error "You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0"
This error shows that file has NO sheet but it is not true. File has data/sheet and it shows upon opening that file.
One interesting fact, if I open the same excel file (manually by double click) and SAVE without making any change, then same code works fine. In my end to end process, I cannot add a manual step to open file every-time to proceed further...
For your information, I thought this is a PHPExcel bug and contacted Mark Backer (coordinator PHPOffice Suit) and he replied with following remarks which seems true.
"My guess would be non-standard namespacing in the file that's generated, which loading and saving in MS Excel fixes"
File Reading Code
$objReader = new PHPExcel_Reader_Excel2007();
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('callsheet.xlsx');
$objPHPExcel->setActiveSheetIndex(0);
$dataArray = $objPHPExcel->getActiveSheet()->toArray(null, true,true,true);
var_dump($dataArray);
Error Trace
Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'You tried to set a sheet active by the out of bounds index: 0. The actual number of sheets is 0.' in E:\xampp\htdocs\podioexcel\Classes\PHPExcel.php:688 Stack trace: #0 E:\xampp\htdocs\podioexcel\test.php(18): PHPExcel-
setActiveSheetIndex(0) #1 {main} thrown in E:\xampp\htdocs\podioexcel\Classes\PHPExcel.php on line 688
Can you help me to address this issue? This is holding up my project completely.
File Path: https://drive.google.com/file/d/0B79S561prrEBUDY1NEhXQ1JySWM/view
Original question at Stackoverflow: Error While loading excel sheet Using phpexcel
Ejaz
require_once('PHPExcel-1.7.7/Classes/PHPExcel.php');
$inputFileName = 'RN Tracker.xlsx';
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
if i try following
$objReader->setIncludeCharts(TRUE);
during load i get following error message:
Warning: PHPExcel_Reader_Excel2007::load(): Node no longer exists in /users/pravkum4/html/tracker/PHPExcel-1.7.7/Classes/PHPExcel/Reader/Excel2007.php on line 1518
Notice: Trying to get property of non-object in /users/pravkum4/html/tracker/PHPExcel-1.7.7/Classes/PHPExcel/Reader/Excel2007.php on line 1519
Fatal error: Call to a member function children() on a non-object in /users/pravkum4/html/tracker/PHPExcel-1.7.7/Classes/PHPExcel/Reader/Excel2007.php on line 1519
What mistake i am doing here?
Should i include some additional class for chart?
Does chart option is included by default in load method of reader? chart count gives 0. :((
Sometimes gives correct chart count but rendering doesn't work.
One more thing, what is this code? not working for me. Sometimes i get jpgraph_pie.php missing.
PHPExcel_Settings::setChartRenderer(
PHPExcel_Settings::CHART_RENDERER_JPGRAPH,
dirname(__FILE__).'/../../libraries/Charts/jpgraph3.5.0b1/src'
);
Please provide a tutorial for chart rendering. Thank you so much in Advance.
I have yet to see this issue raised at the PHPExcel sites on either CodePlex or on Github... and to resolve it you'll need to upload a sample file that demonstrates the problem so that we can identify what node no longer exists in the Excel data
Look at this this may help you...
http://phpexcel.codeplex.com/workitem/16
http://phpexcel.codeplex.com/wikipage?title=Examples