How to wrap PHPExcel generated cells with a link - php

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

Related

Styles and dropdowns are gone

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.

PHPExcel Remove cell (column) removeColumn() not working

i created export PHPExcel, but here i want to remove cell "A" here is piece of my code
$spareparts = $this->spareparts_m->getDataSpareparts();
if ($spareparts) {
foreach ($spareparts as $key => $sparepart)
{
$rownum = $key + $row_geser;
$this->excel->getActiveSheet()->getStyle('A' . $rownum.':W'. $rownum)->getFont()->setSize(10);
if(isset($flip[1])){
$this->excel->getActiveSheet()->setCellValue('A' . $rownum, $sparepart['part_no']);
}else{
$this->excel->getActiveSheet()->removeColumn("A");
}
/* Blah..blah.blah */
but here $this->excel->getActiveSheet()->removeColumn("A");
not working and my excel output became like this screenshot output excel
any solution please ?
Thank you
If you want to remove the column A start it with column B.
something like this...
$this->excel->getActiveSheet()->setCellValue('A', 'Sample');
To: $this->excel->getActiveSheet()->setCellValue('B', 'Sample');
then change the correspond letter to it. you need to set it manually + the row number added from your foreach function.

PHPExcel - reading row comments , text strike missing

Am trying to read excel file with PHPExcel library
https://github.com/PHPOffice/PHPExcel
however when i read row then am not able to get comments on field and strikes on text also missing .
my code is
include 'PHPExcel/IOFactory.php';
$ftype = 'Excel2007';
$fname = 'data.xlsx';
$objexcel = PHPExcel_IOFactory::load($fname);
$sdata = $objexcel->getActiveSheet()->toArray(null,true,true,true);
foreach ($sdata as $k=>$d) {
print_r($d); // output
}
so it output comments and strikes which are in data.xlsx file are missing .
any idea how to display and check if row have comments and strikes through text

Set images for price list via phpexcel

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

PHPEXCEL rows layout

I would like to write into a csv file using PHPEXCEL in the following format. The class in bold and a space after every class group.
But i have not been able to find a way to do it.
Here is what I could do.
I generate the data from an sql and loop thought it. Here is my code
$sql="SELECT * FROM table ORDER BY typeclass";
$query=mysql_query($sql);
$check='';
$i=1;
while($row=mysql_fetch_assoc($query))
{
if($row['class']!=$check)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$row['Class']);
$check=$row['class'];
}
$objPHPExcel->getActiveSheet()->setCellValue('B' .$i,$row['Name']);
$objPHPExcel->getActiveSheet()->setCellValue('C' .$i,$row['score']);
$i++;
}
Can anyone help me get the desired output.
How to format a cell bold:
$styleArray = array('font' => array('bold' => true));
$objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray);
You can insert a new Row after inserting the class name.
if($row['class']!=$check)
{
$objPHPExcel->getActiveSheet()->setCellValue('A'.$i,$row['Class']);
$check=$row['class'];
// Merge the cells with the class name
$objPHPExcel->getActiveSheet()->mergeCells('A'.$i.':B'.$i);
$i++; // Next Row
}
Then you can put the name and score into column A and B:
$objPHPExcel->getActiveSheet()->setCellValue('A' .$i,$row['Name']);
$objPHPExcel->getActiveSheet()->setCellValue('B' .$i,$row['score']);
That should do all the trick. For further questions using PHPEXCEL youre able to contact me via email.
By the way, poor Simon :(

Categories