PHPExcel - How to set a url

Miloš picture Miloš · Apr 16, 2014 · Viewed 28.8k times · Source

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.

Answer

Miloš picture Miloš · Apr 16, 2014

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.