php PHPExcel - 如何设置网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23100636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:29:52  来源:igfitidea点击:

PHPExcel - How to set a url

phpphpexcel

提问by Milo?

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

我是 PHPExcel 并且在字符串中有一个 URL。做的时候:

$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.

url 设置为简单文本。

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.

我注意到在最后设置斜杠时,超链接有效,但 url 是错误的。

回答by Milo?

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

我找到了解决方案,不知何故我的 url 没有被 excel 识别。

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

And now it works. Hope this will help.

现在它起作用了。希望这会有所帮助。

回答by vsingh

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

回答by pedro.caicedo.la

Try to write your code as below line:

尝试将您的代码编写如下:

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

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

回答by Patrick

Just lost an hour on the same issue. Finally (after checking PHPexcel source) figured out, that ->getCell('A1') is not requiredand allways lead me to the following error: Call to a member function getHyperlink() on a non-object.Instead you have to pass the cell-coordinates directly togetHyperlink('A1')like this:

刚刚在同一个问题上浪费了一个小时。最后(在检查 PHPexcel 源代码后)发现,不需要 ->getCell('A1')并且总是导致我出现以下错误:调用非对象上的成员函数 getHyperlink()。相反,您必须像这样直接将单元格坐标传递给getHyperlink('A1')

$YourActiveSpreadsheet->getHyperlink('A1')->setUrl($url);