PhpExcel Reader: Couldn't read certain files - php

Error Shows
->Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object
When uploading files, and reading excel files, sometimes it shows that error message.
This is my code:
$pasFile = $_FILES['inputFileLocation']['name'];
$target_path = basename($pasFile);
if(move_uploaded_file($_FILES["inputFileLocation"]["tmp_name"], $target_path)){
require_once '../template/PHPExcel/Classes/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$worksheet_names = $objReader->listWorksheetNames($pasFile);
$countWorksheet = count($worksheet_names);
$optionSheetName = "<option></option>";
for($x = 0;$x < $countWorksheet;$x++){
$optionSheetName = $optionSheetName."<option value='".$worksheet_names[$x]."'>".$worksheet_names[$x]."</option>";
}
}
Thank you in advance! :)

My mistake! The file that I was uploading had different types of excel format.
This code now works by identifying the format then passing it to the create reader.
Many thanks to Mr. Baker!
I added this codes:
$inputFileType = PHPExcel_IOFactory::identify($pasFile);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
Resulting to this one:
$pasFile = $_FILES['inputFileLocation']['name'];
$target_path = basename($pasFile);
if(move_uploaded_file($_FILES["inputFileLocation"]["tmp_name"], $target_path)){
require_once '../template/PHPExcel/Classes/PHPExcel.php';
$inputFileType = PHPExcel_IOFactory::identify($pasFile);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$worksheet_names = $objReader->listWorksheetNames($pasFile);
$countWorksheet = count($worksheet_names);
$optionSheetName = "<option></option>";
for($x = 0;$x < $countWorksheet;$x++){
$optionSheetName = $optionSheetName."<option value='".$worksheet_names[$x]."'>".$worksheet_names[$x]."</option>";
}
}
identify() first before createReader()...

Related

Read .xlsx and .xls data in codeigniter

I want to read the data of .xlsx or .xls file in codeigniter. I have read the other questions related it but nothing works. I have used phpexcel, reader but with no luck. In my project i give the option to upload excel file then i want to read the data and insert it in the database.
Now i am using phpExcel library I have wrote:
$this->load->library('excel');
$reader= PHPExcel_IOFactory::createReader('Excel2007');
$reader->setReadDataOnly(true);
$path=(FCPATH.'uploads/productfile/'.$_FILES['upload_file']['name']);
$excel=$reader->load($path);
$sheet=$excel->setActiveSheetIndex(0);
for($i=0;$i<=1000;$i++)
{
$col1= $sheet->getCellByColumnAndRow(0,$i)->getValue();
$col2= $sheet->getCellByColumnAndRow(1,$i)->getValue();
$col3= $sheet->getCellByColumnAndRow(2,$i)->getValue();
var_dump($col1);
}
but it display :
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 Please give me some example code.
The error
Please give me some example code:
Try this :
$sheet = $excel->getActiveSheet()->toArray(null,true,true,true);
This will return you an array of the current active sheet.Hope this helps.
This error is caused by a wrong initialization of the PHPexcel reader functionalities I suppose.
I usually do something like this to get the data from an excel:
require_once '../Classes/PHPExcel/IOFactory.php';
$filename = '../uploads/product/abc.xls';
$objPHPExcel = PHPExcel_IOFactory::load($filename);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
$total_rows = $highestRow-1;
for ($row = 2; $row <= $highestRow; ++ $row) {
//id
$cell = $worksheet->getCellByColumnAndRow(0,$row);
$id = $cell->getValue();
if(is_null($id)){$id = '#';}
}
Thanks to all for your suggestion:
I got the solution :
$file_data = $this->upload->data();
$file_path = './uploads/productfile/'.$file_data['file_name'];
include 'Classes/PHPExcel/IOFactory.php';
$inputFileName = $file_path;
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
for($i=2;$i<=$arrayCount;$i++)
{
'product'=$allDataInSheet[$i]["C"],
'brand'=$allDataInSheet[$i]["I"],
'standard'=$allDataInSheet[$i]["J"],
}
This extension of PHPExcel_IOFactory has issues with symbols like ™ Trademark, Copy Right, Degree etc.
You can't read a particular block if it contains such special characters.
This code segment works for me,
$this->load->library('excel');
$reader= PHPExcel_IOFactory::createReader('Excel2007');
$reader->setReadDataOnly(true);
$path= "./media/customer_exports/data-1558003506.xlsx";
$excel=$reader->load($path);
$sheet = $excel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($sheet);
for($i=2;$i<=$arrayCount;$i++)
{
echo $sheet[$i]["A"].$sheet[$i]["B"].$sheet[$i]["C"];
}

Import .xlsx file with PHP

Here my snippet code
require_once 'readernew.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$data->read('InOut.xlsx');
$res = $data->sheets;
var_dump($data);
but it give the error like filename is not readable without the password protection
will you please help to access .xlsx file
You can use PHPExcel to read XLSX.
<?php
$objPHPExcel = PHPExcel_IOFactory::load('InOut.xlsx');
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
var_dump($sheetData);

PHPExcel can't have 2 reader in the same web page?

I'm using PHPExcel to read 3 files. 2 of them are xls and 1 of the is xlsx. I create 2 reader for the task but encountered error.
HTTP Error 500.0 - Internal Server Error
C:\AppServ\php5\php-cgi.exe - The FastCGI process exited unexpectedly
Here is my code
$inputFileType = PHPExcel_IOFactory::identify($conPath);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($conPath);
$objReader = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelShip = $objReader->load("Ship to mapping.xls");
$objPHPExcelMat = $objReader->load("Material Mapping.xls");
The $inputFileType is variable for the type of uploaded file(which is xlsx.). The code works fine if I upload xls. but give error if the uploaded file is xlsx.
Please help.
You can't if you use the same variable for each reader instance: you need to use a separate reader instance for each reader that you instantiate:
$objReader1 = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelShip = $objReader1->load("Ship to mapping.xls");
$objReader2 = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelMat = $objReader2->load("Material Mapping.xls");
or (at least) instantiate a new reader for each file as you load it
$objReader = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelShip = $objReader->load("Ship to mapping.xls");
$objReader = PHPExcel_IOFactory::createReader("Excel5");
$objPHPExcelMat = $objReader->load("Material Mapping.xls");

PHPExcel not working with xls file

I am using PHPExcel to read excel file in php,it is working fine with xlsx but when try to read xls file,It shows an error
Fatal error: Call to undefined method PHPExcel_Reader_CSV::setReadDataOnly() in /var/www/....
Here is my code
$file_path='/var/www/html/site/sample.xls';
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
/** Load $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load( $file_path);
$total_sheets=$objPHPExcel->getSheetCount();
$allSheetName=$objPHPExcel->getSheetNames();
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
for ($row = 1; $row <= $highestRow;++$row)
{
for ($col = 0; $col <$highestColumnIndex;++$col)
{
$value=$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$arraydata[$row-1][$col]=trim($value);
}
}
The file that you're loading is identified as a CSV file, even though it has an .xls extension... it is quite a common practise for some developers to save files formatted as csv or with html markup with a .xls extension, but that doesn't make them BIFF-format .xls files.
The setReadDataOnly() method isn't available for the CSV Reader, because a CSV file cannot contain anything other than data.
The most recent versions of PHPExcel provide a stub for setReadDataOnly() in the CSV Reader to prevent an error in this situation, and I'd certainly recommend that you upgrade to the latest code; but if you can't do that, then the simplest fix for you is simply to wrap the call to setReadDataOnly() in an if test:
$inputFileType = PHPExcel_IOFactory::identify( $file_path);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
if ($inputFileType !== 'CSV') {
$objReader->setReadDataOnly(true);
}
For binary Excel files (xls) you may have better luck using the (old) PHP-ExcelReader. It's the most reliable one I've found so far.

How to split the Excel file that contains multiple sheets?

I have the Excel file that contains a few sheets. How to split this file to get get each sheet as a separate file?
From PHP, you'llneed to have something like phpExcel to open the spreadsheet, and rewrite each tab as a new file.
Using the PHPExcel library:
include 'PHPExcel.php';
$fileType = 'Excel2007';
$inputFileName = 'testExcel.xlsx';
$objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
$objPHPExcel = $objPHPExcelReader->load($inputFileName);
$sheetIndex = 0;
$sheetCount = $objPHPExcel->getSheetCount();
while ($sheetIndex < $sheetCount) {
++$sheetIndex;
$workSheet = $objPHPExcel->getSheet(0);
$newObjPHPExcel = new PHPExcel();
$newObjPHPExcel->removeSheetByIndex(0);
$newObjPHPExcel->addExternalSheet($workSheet);
$objPHPExcelWriter = PHPExcel_IOFactory::createWriter($newObjPHPExcel,$fileType);
$outputFileTemp = explode('.',$inputFileName);
$outputFileName = $outputFileTemp[0].$sheetIndex.'.'.$outputFileTemp[1];
$objPHPExcelWriter->save($outputFileName);
}

Categories