phpexcel can't get not empty value - php

I use phpexcel to read a table,but sometimes it can't get value which is not empty.
import('ORG.PHPExcel');
import('ORG.PHPExcel.IOFactory');
import('ORG.PHPExcel.Reader.Excel5');
import('PHPExcel.Reader.Excel2007');
$objReader = new PHPExcel_Reader_Excel5();
$objReader->setReadDataOnly(true);
//use excel2007
$objPHPExcel = new PHPExcel();
$objPHPExcel = $objReader->load($uploadfile);
$sheet = $objPHPExcel->getSheet(1);
$highestRow = $sheet->getHighestRow();
var_dump($highestRow);
$highestColumn = $sheet->getHighestColumn();
var_dump($highestColumn);
$count=0;
$zzdw = M('zzdw');
for($j=4;$j<=$highestRow;$j++)
{
$count++;
$flag=0;
$b= (string)$objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
if($b==''){
echo('<font color="red"><strong>'.$count.'</strong></font>');
var_dump($b);
exit;
}
}
result
like this, row and col have value,but can't get cell's value.
some files don't have this problem,some have.
file format error?I use ".xls".

Related

copied google sheets and try to fetch data using php but data is not accurate

I have this file
https://docs.google.com/spreadsheets/d/1-U3zAECz0TNF-yLzNdzfDyuMzRVAcJrc6L8wni1lDhc/edit?usp=sharing
I am copy this file
and try to get data from this sheet.
$tempLoc = $file;
Yii::import('ext.phpexcel.XPHPExcel');
$objPHPExcel = XPHPExcel::createPHPExcel();
$inputFileType = \PHPExcel_IOFactory::identify($tempLoc);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$ExcelData = $objReader->load($tempLoc);
$sheet = $ExcelData->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
but when i try to get data from this sheet it will display like
image
and you can see in sheet header is different from this array,can someone please help me to slove it,
i tried with
1.$objReader->setInputEncoding('');
2.$objReader->setInputEncoding('UTF-16LE');

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"];
}

Cant read and manipulate excel data

I'm trying to read an excel.xlsx file. But I just can't, I'm reading about an API called PHPExcel but I can't make it work. This is what I tried:
<?php
// get access to the class
require_once 'Classes/PHPExcel.php';
include 'Classes/IOFactory.php';
$inputFileName = 'teste.xlsx';
// Read your Excel workbook
try{
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
}
catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL, TRUE, FALSE);
echo $rowData[$row];
}
?>
at the end of the for loop, I tried to read or echo the data, but I got errors like: Undefined offset.

loading excelsheet data into db in codeigniter

iam need to load excelsheet data into database using codeigiter.for that i have tried this coding
function excel_data()
{
//include 'PHPExcel/IOFactory.php';
$this->load->library("PHPExcel");
$inputFileName = '../first_data.xls';
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL,
TRUE,
FALSE);
// Insert row data array into your database of choice here
// $this->User_model->add_data($rowData);
}
}
while executing iam getting this error "Error loading file "first_data.xls": Could not open ../first_data.xls for reading! File does not exist." what is the problem in my coding.
what is my mistake.did anyone knows that?

How to read cells without format in php Excel 2007

iam trying to read an Excel File with PHP Reader, i print the information with an echo of the Highest Row and the Highest Colum, but what i get is the last formatted Column&Row and what i need is just the last cell where data was inserted. Here is my code:
require_once '..\..\..\Common\PHPExcel_1.7.9_doc\Classes\PHPExcel\IOFactory.php';
**code code code**
ReadExcelFile();
function ReadExcelFile(){
$inputFileType = 'Excel2007';
$inputFileName = '../ExcelFiles/test.xlsx';
$sheetname = 'Tabelle1';
try {
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadDataOnly(true);
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
}
catch(Exception $e) {
die('Error loading Excel file '.$e->getMessage());
}
$sheet = $objPHPExcel->getSheet(0);
echo $highestRow = $sheet->getHighestRow();
echo $highestColumn = $sheet->getHighestColumn();
}
Iam reading a very extense Macro that is modified once in a while externally (thats why i need to read just only to the last cell where has data) What i did was just a small copy of this excel file and the output was just
"254J"
But my last cell with data is about "210J". Anyone knows how to do it?
$highestRow = $sheet->getHighestDataRow();
$highestColumn = $sheet->getHighestDataColumn();

Categories