Set images for price list via phpexcel - php

I have array of data like:
$products = array(
array('path/to/image1', 'name1', 'description1'),
array('path/to/image2', 'name2', 'description2'),
array('path/to/image3', 'name3', 'description3'),
...
array('path/to/imageN', 'nameN', 'descriptionN'),
);
With this code I generate *.xlsx table:
foreach($products as $row) {
$c = 0;
foreach ($row as $cell) {
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($c, $r, $cell);
$c++;
}
$r++;
}
require_once 'Classes/PHPExcel/IOFactory.php';
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('/path/for/save');
And after that I get a table with path, name and descriptions. Please help me, because I can't get table where instead of pathes I will get images.

Setting a cell to contain 'path/to/image1' will do exactly that, set the cell to contain a string value of 'path/to/image1'.
You need to set images very specifically, as explained in the documentation and shown in the examples:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Image');
$objDrawing->setPath('./path/to/image1.jpg');
$objDrawing->setCoordinates('B2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
You should also note that MS Excel does not store images in a cell. It stores images overlaying a cell, so the image may cover several cells
EDIT
If you need to convert numeric coordinates such as $row=3 and $col=0 to a cell reference like A3:
$cellReference = PHPExcel_Cell::stringFromColumnIndex($col) . $row;
or
$cellReference = $objPHPExcel->getActiveSheet()
->getCellByColumnAndRow($col, $row)
->getCoordinate();

Related

PHPExcel - dynamically check columns in a row

I have a xlsx file where the first row has:
image :
I need to dynamically check how much locales are there (by column name), as it can happen that it can be only one or 5/6 columns.
How to set it?
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($filePath);
$activeSheet = $phpExcelObject->getActiveSheet()->rangeToArray('B1:G1');
dump($activeSheet);die;
at first, you can use PhpSpreadsheet for loading Excel files.
there is example code for counting first row keys :
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
$reader = new \PhpOffice\PhpSpreadsheet\Reader\xlsx();
$spreadsheet = $reader->load($filePath);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
$firstRow = $sheetData[0];
$cnt = 0;
foreach ($firstRow as $value) {
if($value)
$cnt++;
}
//because of first column
$cnt--;
the last line, mines one is because of your first column "key"

Read excel sheet containing merged cells using PHPExcel

I want to read an excel sheet completely and using AJAX send each row to another page for processing. So I have used the following code for converting the excel sheet data into JSON array(Reference PHPExcel example provided in Library):
<?php
error_reporting(E_ALL);
set_time_limit(0);
date_default_timezone_set('Asia/Kolkata');
set_include_path(get_include_path() . PATH_SEPARATOR . 'PHPExcel-1.8/Classes/');
require_once 'PHPExcel/IOFactory.php';
$inputFileType = PHPExcel_IOFactory::identify($fileLocation);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setLoadSheetsOnly("SHEETNAME");
$objPHPExcel = $objReader->load($fileLocation);
$data = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
?>
Here $filelocation is the location of the uploaded file which is to be read for sending the rows individually using AJAX to another page.
I am using $data in javascript as
DataToBeUploaded=<?php echo json_encode($data);?>;
But the excel sheet contains some merged cells so PHPExcel is not able to read the values in these merged cells. Hence values in these cells are read as NULL.
Is there a way where I can use the merged cells' upper left cell value for all of the subsequent cells? (Actually in my case cells are merged vertically only)
Eg.
I have (Assume rows are numbered from 1 and columns from A)
Here PHPExcel reads this as:
data[1][A]='abc'
$data[1][B]='123'
$data[2][A]=''
$data[2][B]='456'
$data[3][A]=''
$data[3][B]='789'
I want the snippet to result in these values:
data[1][A]='abc'
$data[1][B]='123'
$data[2][A]='abc'
$data[2][B]='456'
$data[3][A]='abc'
$data[3][B]='789'
Referring to https://github.com/PHPOffice/PHPExcel/issues/643
I have written the following snippet:
$referenceRow=array();
for ( $row = 2; $row <= $noOfBooks; $row++ ){
for ( $col = 0; $col < 7; $col++ ){
if (!$objPHPExcel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->isInMergeRange() || $objPHPExcel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->isMergeRangeValueCell()) {
// Cell is not merged cell
$data[$row][$col] = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow( $col, $row )->getCalculatedValue();
$referenceRow[$col]=$data[$row][$col];
//This will store the value of cell in $referenceRow so that if the next row is merged then it will use this value for the attribute
} else {
// Cell is part of a merge-range
$data[$row][$col]=$referenceRow[$col];
//The value stored for this column in $referenceRow in one of the previous iterations is the value of the merged cell
}
}
}
This will give the result exactly as required

How to wrap PHPExcel generated cells with a link

The cv column holds filenames of files uploaded to server.
How to wrap the cell values with a link to pull the files from server.
This is my code.
$sql = "SELECT name,email,phone,cv from applicants";
$res = $this->db->query($sql);
$exceldata="";
foreach ($res->result_array() as $row){
$exceldata[] = $row;
}
$this->excel->getActiveSheet()->fromArray($exceldata, null, 'A3');
$this->excel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('B3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('C3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$this->excel->getActiveSheet()->getStyle('D3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
I want the cell to show the value with link pointing to the file.
Desired output of the cells:
ex. href="http://examle.com/uploads/cv/ '. cell_value .'">cellvalue
Set the cell as a hyperlink, as described in the PHPExcel Documentation and shown in the examples like 05featuredemo
$cellValue = 123;
$objPHPExcel->getActiveSheet()
->getCell('E26')
->setValue($cellValue);
$objPHPExcel->getActiveSheet()
->getCell('E26')
->getHyperlink()
->setUrl('http://examle.com/uploads/cv/' . $cellValue)
->setTooltip('Click here to access file');
Have a look at the feature demo in the PHPExcel package, there is an example of how to add a link to a cell.
Here's some mockup code for this:
$yourcell = "A3";
$objPHPExcel->getActiveSheet()->setCellValue($yourcell, 'www.example.net');
$objPHPExcel->getActiveSheet()->getCell($yourcell)->getHyperlink()->setUrl('http://www.example.net');
$objPHPExcel->getActiveSheet()->getCell($yourcell)->getHyperlink()->setTooltip('Navigate to website');

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');

PHPExcel. How to check if current cell is merged with another?

I use PHPExcel to import Excel files to my site. For example, there is two cells, A1 and A2, both merged. Is there a way to to find out is A1 merged to another cell (A2 or other) or not?
$workbook = new PHPExcel;
$sheet = $workbook->getActiveSheet();
$sheet->mergeCells('A1:E1');
$cell = $sheet->getCell('A1');
// Check if cell is merged
foreach ($sheet->getMergeCells() as $cells) {
if ($cell->isInRange($cells)) {
echo 'Cell is merged!'
break;
}
}
I think there isn't better solution because of the way phpexcel stores information about merged cells.
The easiest way is to use the getMergeRange() method.
if ($cell->getMergeRange()) {
// cell is merged
}
I suspect many people will want to know the value of the merged cell, in which case you can refer to the following code:
if (($range = $cell->getMergeRange()) && !$cell->isMergeRangeValueCell()) {
$first_in_range_coordinates = strtok($range, ':');
$value = $sheet->getCell($first_in_range_coordinates)->getValue();
}

Categories