PHPExcel - 如何设置网址

时间:2014-04-16 05:23:01

标签: php phpexcel

我是PHP,并且在字符串中有一个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);

网址设置为简单文字。

我也尝试过:

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

但是,当点击该链接时,它会尝试打开一个本地文件夹。

知道怎么做吗?

谢谢。

修改

当我尝试这样做时没有引号:

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

然后我收到错误:

Exception' with message 'Invalid parameters passed.'

我的真实网址是

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

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

4 个答案:

答案 0 :(得分:20)

我找到了解决方案,不知怎样,我的网址没有被excel识别。

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

现在它有效。希望这会有所帮助。

答案 1 :(得分:12)

我猜你的字段值有整数值。如果是这样,那么首先必须转换该单元格的数据类型,然后设置超链接。以下是我如何做到这一点。

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

答案 2 :(得分:3)

尝试编写代码如下:

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

答案 3 :(得分:0)

在同一个问题上错过了一个小时。最后(在查看PHPexcel源代码之后)发现, - > getCell(' A1')不是必需的 ,总是引发我出现以下错误:在非对象上调用成员函数getHyperlink()。 相反,您必须将单元格坐标直接传递给 getHyperlink(' A1'),如下所示:

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