How to read vlookup from PHPExcel - php

I've found that PHPExcel can read vlookup value if it starts with an alphabet, but when it comes to numbers, the value returns #N/A.
How can I make the PHPExcel read numbers value vlookup?
I tried changing the cell format of the vlookup table range to text, but that doesn't do anything.
Here's the code:
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $this->excel = $objReader->load($file_path);
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet); // Here get total count of row in that Excel sheet
for($z=2;$z<=$arrayCount;$z++)
{
$a = $allDataInSheet[$z]["A"];
$b = $allDataInSheet[$z]["B"];
$c = $allDataInSheet[$z]["C"];
echo "a: ".$a."<br/>";
echo "b: ".$b."<br/>";
echo "c: ".$c."<br/>";
}
the result:
a: #N/A
b: Orange Juice
c: Z02
the excel:
Code Description Item
------------ -------------- -----------
3000000073 Orange Juice Z02
where column code and item are a vlookup formula, while description is an input text.

Related

PHPSpreadsheet reads certain dates incorrectly

Ok, so i have an excel file "file1.xlsx" formatted like this:
A B C
1 xxxx 4/30/2019 xxxxx
2 xxxx 5/31/2019 xxxxx
And if I try using PHPSpreadsheet to read the column B like this:
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load('file1.xlsx');
$sheet = $spreadsheet->getActiveSheet();
for ($i = 1; $i <= $sheet->getHighestRow(); $i++) {
echo $sheet->getCell("B$i") . " ";
}
I get this result:
43585 43616
I have no idea what could be causing this. These numbers do not appear anywhere in the spreadsheet, and after some testing I noticed that it doesn't happen on all dates, just some. Can anyone let me know what could be causing this and/or what I can do to fix it? Thank you.

getHighestDataColumn on phpexcelspreadsheet as count in php [duplicate]

This question already has answers here:
PHPExcel how to get column index from cell
(3 answers)
Closed 2 years ago.
I am using https://phpspreadsheet.readthedocs.io/en/latest/ phpspreadsheet to import files to database.I need to get the total number of columns in the uploaded excel before importing it to database.
I found getHighestDataColumn() which returns the highest coulmn as alphabets.
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$high=$spreadsheet->setActiveSheetIndex(0)->getHighestColumn();
dump($high);
die;
which gives output as I
Is there any way to get it as numbers ..??
If you only have A-Z you can convert the chars to ascii and get their int value using ord.
// Get ascii offset for alphabet
$start = ord("A");
$current = "I";
// Calculate char position from offset A
echo ord($current) - $start;
Found the solution as
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($fileName);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
dump($highestColumnIndex);
die;
This will output the index of highestColumn ..

How to get currency sign of cell-content using PhpSpreadsheet

I have cell content looks like "$ 1440" or "€ 970"
But on formula-field (after select cell) value looks like simple number "1440" or "970".
How can I get currency sign ($ or €) by using PhpSpreadsheet?
I tried code
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setReadDataOnly(TRUE);
$spreadsheet = $reader->load('some.xlsx');
$worksheet = $spreadsheet->getActiveSheet();
$a = $worksheet->getStyle(10, 3)->getNumberFormat()->getFormatCode();
$b = $worksheet->getCellByColumnAndRow(10, 3)->getValue();
$c = $worksheet->getCellByColumnAndRow(10, 3)->getFormattedValue();
Got:
a - 'General' (string)
b - 1440 (integer)
c -'1440' (string)
Why is format "General" if it "Finance" actually?
You use $reader->setReadDataOnly(TRUE);.
That meant ignore formetting.
Set it to $reader->setReadDataOnly(FALSE);

phpexcel insert multiple images in merged cells

Forgive my poor English,I'm a programmer from Asia.
I want to insert multiple images in merged cells,but all the images are overlapping.So I write code like this:
//merge cells
$column = 0;
$cell = $position[$column].$row;
$merge_str = $position[$column] . $row . ":" . $position[$column] . $last_row;
$objExcel->getActiveSheet()->mergeCells($merge_str);
$cell_value = '';
$objExcel->setExcelFontFormat($cell, $cell_value, $font_size, false, 'left', 'center');
$offSetY = 10;
//loop $export_data_item['images_path'] ,$image_nums is the mount of images
for($i=0;$i<$image_nums;$i++){
if(file_exists($export_data_item['images_path'][$i])){
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($export_data_item['images_path'][$i]);
$objDrawing->setOffsetX(10);
$objDrawing->setOffsetY($offSetY);
$objDrawing->setRotation(15);
$objDrawing->setHeight($export_data_item['images'][$i]['height']);
$objDrawing->setWidth($export_data_item['images'][$i]['width']);
$objDrawing->setCoordinates($cell);
$objDrawing->setWorksheet($objExcel->getActiveSheet());
$offSetY = $export_data_item['images'][$i]['height'] + $offSetY + 10;
}
}
I hope use 'offsetY' to space every images in vertical direction,but all images squeezed together.I think the reason is that I use "$objDrawing->setCoordinates($cell);", all images only in the $cell position.
I want to set all images be arranged according to the sequence and interval.Someone can help me?
Reason is (according to this) that OffsetY is specific to the cell.
Unfortunately I can't think of a solution. Ask them maybe.
This function works for me. It returns an array with Coordinate and OffsetX from a desired Position X and row number.
Maybe someone helps.
function getCoordOffx($posX, $row) {//Parameters: (desired Position X, row number)
global $objPHPExcel;
$cpos=$widthpix=0;//Set to 0 Current position and width pixels
$col=1;//First column
$colname="A";//If posX=0 not enter in while, and assign colname=A
while ($posX>$cpos) {
$colname=chr(64+$col);//Convert column to Letter. chr(65)=A
$width=$objPHPExcel->getActiveSheet()->getColumnDimension($colname)->getWidth(); //Get width of Column
$font=$objPHPExcel->getActiveSheet()->getStyle($colname.$row)->getFont();//Get Font of column and row
$widthpix= PHPExcel_Shared_Drawing::cellDimensionToPixels($width,$font); // convert to pixels
$cpos+=$widthpix;//Add pixels of current column to cpos
$col++;//Next column
}
$offsX=(int)($posX-($cpos-$widthpix));//Offset is Desired Position X minus start of current column
return array($colname,$offsX);//Returns Column Name Letter (A or C or E...) and OffsetX
}
$coAndOf=getCoordOffx(195,3); //Desired Position X=195, row = 3
$colL=$coAndOf[0];
$offX=$coAndOf[1];

PHPExcel - read time value from a cell

I'm loading an Excel file that has cells with time data, e.g. 08:00:00. But when I try to read those cells with getValue(), it returns some floating point numbers instead of the actual time (in case of 08:00:00, it returns 0.3333333). Here's my code:
$objPHPExcel = PHPExcel_IOFactory::load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
echo $objWorksheet->getCellByColumnAndRow(3, 5)->getValue();
How do I bypass this weird conversion?
PHPExcel 1.7.6 and Excel 2003 Worksheet (.xls)
You need to apply cell format for this:
$cell = $objWorksheet->getCellByColumnAndRow(3, 5);
$cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
echo $cell_value;

Categories