0

I am printing some url which is coming dynamic in phpexcel

$sheet->setCellValue('M'.($results+2),($result['headline']).$result['url']);

but the output is like this Govt to start Air India roadshows in Singapore this weekhttp://www.windowtonews.com/news.php?id=288115

How can i write so that link comes on text with hyperlink

Utsaww
  • 13
  • 3
  • May be duplicate of https://stackoverflow.com/q/23100636/1483629 – Vinay Patil Nov 22 '19 at 09:35
  • sorry but it is not duplicate question as url is dynamic also i am using setCellValue – Utsaww Nov 22 '19 at 09:38
  • Possible duplicate of [PHPExcel - How to set a url](https://stackoverflow.com/questions/23100636/phpexcel-how-to-set-a-url) – Dave Nov 22 '19 at 11:27
  • Your question is in fact a duplicate. Just because the answer happened to use a hard coded value doesn't mean it won't work with a variable name. – Dave Nov 22 '19 at 11:29

1 Answers1

0

You could do this in three steps:

  • set cell value
  • set datatype of this cell to string2
  • set url link to this text value
$sheet->setCellValue('M'.($results+2),$result['headline']);
$sheet->getCell('M'.($results+2))->setDataType(PHPExcel_Cell_DataType::TYPE_STRING2);
$sheet->getCell('M'.($results+2))->getHyperlink()->setUrl(strip_tags($result['url']));

In one line it would looks like:

$sheet->setCellValueExplicit('M'.($results+2), $result['headline'], PHPExcel_Cell_DataType::TYPE_STRING2, TRUE)
      ->getHyperlink()
      ->setUrl(strip_tags($result['url']));

setCellValueExplicit() - similar to getCell(), if setted to true returns the cell instead of the sheet (similar to getActiveSheet()).

Aksen P
  • 4,564
  • 3
  • 14
  • 27