Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPExcel - How to set a url

Tags:

php

phpexcel

I am isung PHPExcel and have a URL in a string. When doing:

$url = 'http://dx.doi.org/10.1016/j.phymed.2005.11.003'
$xls = new PHPExcel();
$xls->setActiveSheetIndex(0);
$xls->getActiveSheet()->setCellValueByColumnAndRow(1,2,$url);

The url is set as simple text.

I also tried:

$xls->getActiveSheet()->getCellByColumnAndRow(1,2)->getHyperlink()->setUrl('"'.$url.'"');

But then, when clicking on the link, it tries to open a local folder.

Any idea how to do that?

Thank you.

EDIT

When I try do do this without quotes:

$xls->getActiveSheet()->getCellByColumnAndRow(1,2)->getHyperlink()->setUrl($url);

Then I am getting the error:

Exception' with message 'Invalid parameters passed.'

My real url is

http://dx.doi.org/10.1016/j.phymed.2005.11.003

I noticed that when setting a slash at the end, the hyperlink works, but the url is then wrong.

like image 542
Milos Cuculovic Avatar asked Apr 16 '14 05:04

Milos Cuculovic


3 Answers

I have found the solution, somehow the url I had was not recognized by excel.

$url = str_replace('http://', '', $link);
$xls->getActiveSheet()->getCellByColumnAndRow(1,2)->getHyperlink()->setUrl('http://www.'.$url);

And now it works. Hope this will help.

like image 136
Milos Cuculovic Avatar answered Oct 15 '22 02:10

Milos Cuculovic


I am guessing your field value has integer value. If it is so, then you first have to convert the data type of that cell and then set the hyperlink. Below is how I have done this.

//set the value of the cell
$this->phpExcelObj->getActiveSheet()->SetCellValue('A1',$id);
//change the data type of the cell
$this->phpExcelObj->getActiveSheet()->getCell("A1")->setDataType(PHPExcel_Cell_DataType::TYPE_STRING2);
///now set the link
$this->phpExcelObj->getActiveSheet()->getCell("A1")->getHyperlink()->setUrl(strip_tags($link));
like image 21
vsingh Avatar answered Oct 15 '22 03:10

vsingh


Try to write your code as below line:

$objSheet->setCellValue('A1', '=Hyperlink("https://www.someurl.com/","Mi web")');

like image 12
pedro.caicedo.dev Avatar answered Oct 15 '22 03:10

pedro.caicedo.dev