PHPExcel - How to set a url

前端 未结 4 788
Happy的楠姐
Happy的楠姐 2021-02-04 06:53

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->setActiveSh         


        
相关标签:
4条回答
  • 2021-02-04 06:53

    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));
    
    0 讨论(0)
  • 2021-02-04 06:54

    Just lost an hour on the same issue. Finally (after checking PHPexcel source) figured out, that ->getCell('A1') is not required and 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 to getHyperlink('A1') like this:

    $YourActiveSpreadsheet->getHyperlink('A1')->setUrl($url);
    
    0 讨论(0)
  • 2021-02-04 07:10

    Try to write your code as below line:

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

    0 讨论(0)
  • 2021-02-04 07:13

    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.

    0 讨论(0)
提交回复
热议问题