首页 > 解决方案 > 如何在 phpexcel 中编写连接 url nd 文本

问题描述

我正在打印一些在phpexcel中动态的url

$sheet->setCellValue('M'.($results+2),($result['headline']).$result['url']);

但输出是这样的Govt to start Air India roadshows in Singapore this weekhttp://www.windowtonews.com/news.php?id=288115

我怎么写才能使链接出现在带有超链接的文本上

标签: phpphpexcelphpexcel-1.8.0

解决方案


你可以通过三个步骤来做到这一点:

  • 设置单元格值
  • 将此单元格的数据类型设置为string2
  • 设置此文本值的 url 链接
$sheet->setCellValue('M'.($results+2),$result['headline']);
$sheet->getCell('M'.($results+2))->setDataType(PHPExcel_Cell_DataType::TYPE_STRING2);
$sheet->getCell('M'.($results+2))->getHyperlink()->setUrl(strip_tags($result['url']));

在一行中,它看起来像:

$sheet->setCellValueExplicit('M'.($results+2), $result['headline'], PHPExcel_Cell_DataType::TYPE_STRING2, TRUE)
      ->getHyperlink()
      ->setUrl(strip_tags($result['url']));

setCellValueExplicit() - 类似于 getCell(),如果设置为true返回单元格而不是工作表(类似于 getActiveSheet())。


推荐阅读