我是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);
Run Code Online (Sandbox Code Playgroud)
网址设置为简单文本.
我也尝试过:
$xls->getActiveSheet()->getCellByColumnAndRow(1,2)->getHyperlink()->setUrl('"'.$url.'"');
Run Code Online (Sandbox Code Playgroud)
但是,当点击链接时,它会尝试打开本地文件夹.
知道怎么做吗?
谢谢.
编辑
当我尝试这样做时,没有引号:
$xls->getActiveSheet()->getCellByColumnAndRow(1,2)->getHyperlink()->setUrl($url);
Run Code Online (Sandbox Code Playgroud)
然后我收到错误:
Exception' with message 'Invalid parameters passed.'
Run Code Online (Sandbox Code Playgroud)
我的真实网址是
http://dx.doi.org/10.1016/j.phymed.2005.11.003
Run Code Online (Sandbox Code Playgroud)
我注意到,当在末尾设置斜杠时,超链接可以工作,但是url是错误的.
Mil*_*loš 20
我找到了解决方案,不知何故我的网址没有被excel识别.
$url = str_replace('http://', '', $link);
$xls->getActiveSheet()->getCellByColumnAndRow(1,2)->getHyperlink()->setUrl('http://www.'.$url);
Run Code Online (Sandbox Code Playgroud)
现在它有效.希望这会有所帮助.
小智 13
我猜你的字段值有整数值.如果是这样,那么首先必须转换该单元格的数据类型,然后设置超链接.以下是我如何做到这一点.
//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));
Run Code Online (Sandbox Code Playgroud)
尝试将您的代码编写如下:
$objSheet->setCellValue('A1', '=Hyperlink("https://www.someurl.com/","Mi web")');