Create Dataset/Array for VLOOKUP Using PHPSpreadsheet - php

I have a export.php file with a goal of setting data validation with a VLOOKUP function off of that. However, it seems I can't find anything in the Recipes that actually seem to support this. I thought I found something but it doesn't seem to work, or at least, it doesn't work how I'd like.
Here is my simplified code:
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$i = 3;
foreach ($_POST['foo'] as $key => $el)
{
$sheet->setCellValue('D'. $i, '=VLOOKUP(C'. $i .',array,2,0)');
$i++;
}
# Column A = labels
# Column B = value
# create range
$spreadsheet->addNamedRange(new \PhpOffice\PhpSpreadsheet\NamedRange('array', $spreadsheet->getActiveSheet(), 'A'));
$spreadsheet->addNamedRange(new \PhpOffice\PhpSpreadsheet\NamedRange('array', $spreadsheet->getActiveSheet(), 'B'));
# redirect browser output -> php://output (download)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="hello.xlsx"');
header('Cache-Control: max-age=0');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
However, this doesn't work - the cell ranges fatal error as you can't select a column to add to the range.
How do I create a dataset for VLOOKUP using PHPSpreadsheet?
Thanks

The answer is to not use a dataset as such, but use a cell range:
$lastValueCell = 84; # this is dynamically got, but no value in showing how
$output[$partNum][3] = '=VLOOKUP(C'. $x .',Formulas!A$1:B$'. $lastValueCell .',2,0)';
# then append to spreadsheet using ->setCellValue()
VLOOKUP now works on export

Related

How to add new row on Excel using PHPSpreadsheet

Hi I'm new to this library called PHPSpreadsheet.
I tried reading it's docs but I can't understand it.
I want to insert a new row on an existing Excel File and
here is what I have so far:
<?php
require '../vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$inputFileName = 'Excel/hello.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Updated');
$writer = new Xlsx($spreadsheet);
$writer->save('../controller/excel/hello.xlsx');
?>
This inserts new data on the 'hello.xsls' file replacing the cell's previous data. How can I make it write data into a new row?
To create a new row, you need to call insertNewRowBefore() with the row number you want to insert before...
$sheet = $spreadsheet->getActiveSheet();
$sheet->insertNewRowBefore(1);
$sheet->setCellValue('A1', 'Updated');
You can also call it with a number of rows to insert, the default is 1.
If you want to append a row, you can call getHighestRow() to find the last row and add 1 to it for the new row. Also change the hard coding of the column in the setCellValue() call to use this row as well...
$sheet = $spreadsheet->getActiveSheet();
$row = $sheet->getHighestRow()+1;
$sheet->insertNewRowBefore($row);
$sheet->setCellValue('A'.$row, 'Updated');
Hello I think this issue came because of memory exhausted so increase your memory limit by php.ini file
like memory_limit = 256M
After increase memory limit , restart your apache
The accepted answer throws exception:
"Column references should not be numeric."
For me works:
...
$row = 'A' . ($sheet->getHighestRow() +1);
$sheet->insertNewColumnBefore($row);
$sheet->setCellValue($row, 'Hello World again!');
...

Generate .xlsx file using fromArray for a big amount of data

I need to write in a .xlsx file about 111.100 rows, using fromArray() but I have a strange error
I use phpspreadsheet library
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$columnLetter = 'A';
foreach ($columnNames as $columnName) {
// Allow to access AA column if needed and more
$sheet->setCellValue($columnLetter.'1', $columnName);
$columnLetter++;
}
$i = 2; // Beginning row for active sheet
$columnLetter = 'A';
foreach ($columnValues as $columnValue) {
$sheet->fromArray(array_values($columnValue), NULL, $columnLetter.$i);
$i++;
$columnLetter++;
}
// Create your Office 2007 Excel (XLSX Format)
$writer = new Xlsx($spreadsheet);
// In this case, we want to write the file in the public directory
// e.g /var/www/project/public/my_first_excel_symfony4.xlsx
$excelFilepath = $directory . '/'.$filename.'.xlsx';
// Create the file
$writer->save($excelFilepath);
And I get the exception :
message: "Invalid cell coordinate AAAA18272"
#code: 0
#file: "./vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Cell/Coordinate.php"
Can you help me please ?
Excel pages are limited. The limit is huge but still limited. This is a correct filter so you can't write if there is no space for it.
Anyway you shouldnt use excel pages for such a big amount of data, you can try fragmenting it into smaller pieces, but databases should be the way to manipulate such amount of information

PHPSpreadsheet, Drawing - move and size with cells

I try to set the setting "Move and size with cells" for my images in PHPSpreadsheet, but cant find any information anywhere. Now my question is, if that feature is implemented yet, and if it is, how its called.
Maybe is too late for "help YOU" but this will be useful for another people
I had the same problem and found 2 "things"
Nothing
Google is a bunch of millons links/sublinks to 50 top-web. Google is the old AOL of 1998
But, I didn't give up and I could "fix" it. Actually, in the folder of PHPSpreadsheet you will see the next next path folder;
/PhpOffice/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx
There're a file called: Drawing.php, in this file you will find a function called: writeDrawing
there you will find the following code;
[Line: 152]
$objWriter->startElement('xdr:oneCellAnchor');
It must be replace with:
$objWriter->startElement('xdr:twoCellAnchor');
After, 2 o 3 lines, under those code you will find this code;
[Line:166 to 169]
$objWriter->startElement('xdr:ext');
$objWriter->writeAttribute('cx', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getWidth()));
$objWriter->writeAttribute('cy', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getHeight()));
$objWriter->endElement();
Add comment marks like "//" or "/* code */" and comment it for avoid these code lines. And now, add the following code;
$objWriter->startElement('xdr:to');
$objWriter->writeElement('xdr:col', $aCoordinates[0] - 1);
$objWriter->writeElement('xdr:colOff', \PhpOffice\PhpSpreadsheet\Shared\Drawing::pixelsToEMU($pDrawing->getWidth()));
$objWriter->writeElement('xdr:row', $aCoordinates[1]);
$objWriter->writeElement('xdr:rowOff', "0");
$objWriter->endElement();
In the following code, should be fix
[Line:178 to 182]
$objWriter->startElement('xdr:cNvPr');
$objWriter->writeAttribute('id', $pRelationId);
$objWriter->writeAttribute('name', $pDrawing->getName());
$objWriter->writeAttribute('descr', $pDrawing->getDescription());
$objWriter->endElement();
the fixed code (don't add "$objWriter->endElement()");
$objWriter->startElement('xdr:cNvPr');
$objWriter->writeAttribute('name', $pDrawing->getName() . $pRelationId);
$objWriter->writeAttribute('descr', $pDrawing->getDescription());
$objWriter->writeAttribute('id', $pRelationId+1);
At Now, in the [Line: 183] INSERT this code;
$objWriter->startElement('a:extLst');
$objWriter->startElement('a:ext');
$objWriter->writeAttribute('uri', "{FF2B5EF4-FFF2-40B4-BE49-F238E27FC236}");
$objWriter->endElement(); // this close a:ext
$objWriter->endElement(); // this close a:extLst
$objWriter->endElement(); // thise xdr:cNvPr [this will replace the $objWriter->endElement(); of Line 182. It was lost in the previous step
Now, a code like this;
require "../../PhpOffice/vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
...
$imgfile = base64_decode($img_from_query_sql);
$fname = "../tmp/".rand(). ".xlsx";
file_put_contents($fname, $imgfile);
$drawing = new PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$Cell = $Letter[$j] . $row;
$drawing->setPath($fname);
$drawing->setCoordinates($Cell );
$drawing->setResizeProportional(false);
$drawing->setWidth(300);
$drawing->setHeight(300);
$drawing->setWorksheet($sheet);
$sheet->setTitle("TitleOfSheet");
$writer = new Xlsx($spreadsheet);
$writer->save("php://output");
...
Execute and download your excel with images. Check your images: right click->"Size and properties..."
Will appear a right-panel of "Image format". In sub-menu "properties" should be a 3 "radio option" (circle with black point)
Move and size with cells
Move but don't size with cells
Don't move or size with cells
Phpspreadsheet orignally, makes pictures with option 2-> Move but don't size with cells but, we changed the code of Drawing.php and now all images are created with option 1->Move and size with cells
At now, if we have a excel file with a list of "things" and 1 image per row, we can apply filters and if some rows doesn't have any image the following image-row will not overlapping over the visible row (matching results).
Each picture filtered will be hidden with is parent row, and each picture matching with filter will be 'stick' to his parent row as always should have been
UPDATE: 28-07-22 - SpreadSheet 1.23
In vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/Xlsx/Drawing.php still exist a function called writeDrawing in line 184;
This functions has changed a bit since the last version I used. Now, writeDrawing have many improves and doesn't require edit any codeline;
Currenly, "BaseDrawing" is a data type that have a new method called;
setCoordinates2 (two) instead of setCoordinates (withtout "2" at end)
You can use setCoordinates2 in your main_custom_file_excel_creator.php like;
$Cell = "A2";
$drawing->setCoordinates2($Cell); //instead of setCoordinates
The updated function (writeDrawing) from line 184 to 340, will use $drawing. This $drawing variable is type BaseDrawing so, in line 187 it will check if is set or not and will store a boolean in $isTwoCellAnchor
It will help us for some setting like #Wind Cloud was looking. Can be solved doing a simple;
$drawing->setEditAs("absolute");
The new version 1.23 (2022-jul-28) have better class Drawing;
Now you can do it without any edit. You only need set the correct methods;
for Move but don't size with cells
require "../../PhpOffice/vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
...
$imgfile = base64_decode($img_from_query_sql);
$fname = "../tmp/somefile123.xlsx";
file_put_contents($fname, $imgfile);
$drawing = new PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$Cell = "A2";
$drawing->setPath($fname);
$drawing->setCoordinates2($Cell ); //USE THE NEW METHOD SETTER
$drawing->setResizeProportional(false);
$drawing->setWidth(300);
$drawing->setHeight(300);
$drawing->setWorksheet($sheet);
$sheet->setTitle("TitleOfSheet");
$writer = new Xlsx($spreadsheet);
$writer->save("php://output");
A user called Wind Cloud asked;
I want to set "Don't move or size with cells" instead. Do you know
how? – Wind Cloud
In the new version ^1.23 will be very easy;
require "../../PhpOffice/vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
...
$imgfile = base64_decode($img_from_query_sql);
$fname = "../tmp/somefile123.xlsx";
file_put_contents($fname, $imgfile);
$drawing = new PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$Cell = "A2";
$drawing->setPath($fname);
$drawing->setCoordinates**2**($Cell);
$drawing->setEditAs("absolute"); //HERE IS YOUR KEY
$drawing->setResizeProportional(false);
$drawing->setWidth(300);
$drawing->setHeight(300);
$drawing->setWorksheet($sheet);
$sheet->setTitle("TitleOfSheet");
$writer = new Xlsx($spreadsheet);
$writer->save("php://output");

append Indian currency symbol after export excel sheet using PhpOffice PhpSpreadsheet?

I am working on "export to excel" sheet functionality using "PhpOffice\PhpSpreadsheet" library, till export to excel sheet working fine but My Problem here is that I have a total amount column where I have to add Indian currency symbol. I searched for a solution but no luck yet.
My Code:
require_once('../vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$objPHPSpreadSheet = new Spreadsheet();
$sheet = $objPHPSpreadSheet->getActiveSheet();
$results=getsalesReport($data);
$sheet->setCellValue('A1', 'title1');
$sheet->setCellValue('B1', 'title2');
$sheet->setCellValue('C1', 'title3');
$rowCount = 2;
if(isset($results) && !empty($results)){
foreach ($results as $key => $val) {
$total_amount = '₹'.$val['TotalAmount']; // I added '₹' for INR currency symbol, its working fine in HTML
$sheet->setCellValue('A'.$rowCount, $val['my_val1']);
$sheet->setCellValue('B'.$rowCount, $val['my_val2']);
$sheet->setCellValue('C'.$rowCount, $total_amount);
$rowCount++;
}
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename=sales_report.xlsx');
header('Cache-Control: max-age=0');
$writer = new Xlsx($objPHPSpreadSheet);
$writer->save("php://output");
How to add Indian currency symbol to total_amount cell in Excel sheet? Hope someone helps. Thanks.

PHPExcel - Combine two file in one

I have two xlsx files: first.xlsx and second.xlsx, I would combine this two files in one and color the second part of the last file appended(second.xlsx). What can I do?
Open/load both files as two separate PHPExcel objects, and use the addExternalSheet() method to move sheets from the second PHPExcel object to the first, then colour to taste and save the first.
$objPHPExcel1 = PHPExcel_IOFactory::load("MergeBook1.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("MergeBook2.xlsx");
foreach($objPHPExcel2->getSheetNames() as $sheetName) {
$sheet = $objPHPExcel2->getSheetByName($sheetName);
$sheet->setTitle($sheet->getTitle() . ' copied');
$objPHPExcel1->addExternalSheet($sheet);
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save('mergedBooks.xlsx');
The use of addExternalSheet() ensures that all styling, merges, etc as well as cell data is transferred over from the second workbook to the first
You can do whatever additional styling and colouring you want after the merge loop, and before saving
EDIT
If you simply want to copy data from one workbook to another, then something like:
$objPHPExcel1 = PHPExcel_IOFactory::load("MergeBook1.xlsx");
$objPHPExcel2 = PHPExcel_IOFactory::load("MergeBook2.xlsx");
$objPHPExcel1->getActiveSheet()->fromArray(
$objPHPExcel2->getActiveSheet->toArray(),
null,
'A' . ($objPHPExcel1->getActiveSheet()->getHighestRow() + 1)
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel1, 'Excel2007');
$objWriter->save('mergedBooks.xlsx');

Categories