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");
Related
Using PHPSpreadsheet, when populating an existing spreadsheet, all styles, conditional formatting, dropdowns, etc. are gone.
How can I retain the original settings while writing to php://output?
Here's a screenshot of what the base ODS file should look like:
When opening this template and writing values from the database to the R/A/S/C/I cells, the outcome is this:
Here is the code I have so far. You can ignore $modulePath etc. as that is just the lookup of the full system path to the file.
$original = $modulePath . '/resources/ISO27k-RASCI-tool.ods';
$reader = IOFactory::createReader('Ods');
$sheet = $reader->load($original);
$sheet->setActiveSheetIndex(1);
$sheet->setHasMacros(true);
$col = range('A', 'Z');
$teams = $this->dataRecord->Annex()->Teams();
foreach ($teams as $i => $team) {
$sheet->getActiveSheet()->setCellValue($col[$i + 2] . '1', $team->Name);
}
$writer = new Ods($sheet);
$writer->save('php://output');
Be aware of that not all excel/ods styling commands are implemented in phpspreedsheet.
As well there is no "pivottable" and "format as table" support.
Your table looks like you use at least "format as table".
The problem is that you crate a new file and your file is only a kind of template. Unsupported cell styling will be skipped. The only solution is that you must style your output with the styling commands.
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!');
...
I create in my php code a excel file with one tab and give it a name.
The code what i use is this.:
// CREATE PHPSPREADSHEET OBJECT
require "../vendor/autoload.php";
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
// CREATE A NEW SPREADSHEET + POPULATE DATA
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Batch');
But how do i create a second tab with another name?
[solved]
// CREATE A NEW SPREADSHEET + POPULATE DATA
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Batch');
// Add some data
$spreadsheet->createSheet();
// Add some data
$spreadsheet->setActiveSheetIndex(1) ->setCellValue('A1', 'world!');
// Rename worksheet
$spreadsheet->getActiveSheet()->setTitle('URL Removed');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);
You can add an extra tab like this:
$spreadsheet->createSheet();
// Zero based, so set the second tab as active sheet
$spreadsheet->setActiveSheetIndex(1);
$spreadsheet->getActiveSheet()->setTitle('Second tab');
If you like, you can read more here.
You can also try this way
//at the beginning of the code
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Extra Tab');
$spreadsheet->addSheet($myWorkSheet, 0);
$sheetIndex = $spreadsheet->getIndex(
$spreadsheet->getSheetByName('Extra Tab')
);
$spreadsheet->setActiveSheetIndex($sheetIndex);
For more information here
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
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');