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"];
}
Related
I tried to read a .csv file using the PHPExcel library but it is giving me question marks instead of the Hebrew text.
The problem only occur when I tried to upload .csv files created from Windows Operating System. I tried the same for MacOS and Linux and the data seems to be fine. How can I solve the encoding issue from windows
public function actionUpload()
{
$params = $_FILES['uploadFile'];
if($params)
{
$data = array();
$model = new UploadForm();
$model->uploadFile = $_FILES['uploadFile'];
$file = UploadedFile::getInstanceByname('uploadFile');
$inputFileName = $model->getpath($file,$data);
// Read your Excel workbook
try
{
$inputFileType = \PHPExcel_IOFactory::identify($inputFileName['link']);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName['link']);
}
catch(Exception $e)
{
die('Error loading file "'.pathinfo($inputFileName['link'],PATHINFO_BASENAME).'": '.$e->getMessage());
}
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$fileData = array();
// 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);
array_push($fileData,$rowData[0]);
// Insert row data array into your database of choice here
}
return $fileData;
}
}
The output array has "???? ???" in places where Hebrew text was present when .csv files created from Windows was uploaded.
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');
I 'm using ms excel to upload large data to my system. When I upload excel file which has columns made by recipe.The uploading progess is longer than usual.Now,I want to check valid the input excel file.If they have columns made by recipe, the system will cancel progess. I 've read phpexcel doucment, but not found any solution.
Do you have any ideas?
My source
My source:
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($uploaded_dir . DIRECTORY_SEPARATOR . $new_name);
$objWorksheet = $objPHPExcel->setActiveSheetIndex(0);
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$total_order_row = $highestRow;
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
for ($i = 1; $i <= $highestRow; $i++) {
$data1 = $objWorksheet->getCellByColumnAndRow(0, $i)->getValue();
$data2= $objWorksheet->getCellByColumnAndRow(1, $i)->getValue();
$data3= $objWorksheet->getCellByColumnAndRow(2, $i)->getValue();
//Todo: insert to db
}
If "by recipe" you mean by a calculation, then check the datatype of a cell rather than the value:
if ($objWorksheet->getCellByColumnAndRow(1, $i)->getDataType() == PHPExcel_Cell_DataType::TYPE_FORMULA) {
echo 'I contain a formula; use getCalculatedValue() to get my value';
}
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();
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);
}