【问题标题】:PHPExcel str_replacePHPExcel str_replace
【发布时间】:2017-05-17 06:47:44
【问题描述】:

我想将我的 excel 超链接路径转换为使用 PHPExcel 打开链接。

$links = $objPHPExcel->getActiveSheet()->getHyperlinkCollection();

此方法将返回一个超链接对象数组,以单元格地址为索引;然后您可以使用带有适当回调的 array_filter() 并设置 ARRAY_FILTER_USE_KEY 标志来提取特定范围内的那些。

我的 var_dump($links);输出:

但我不知道如何使用 array_filter() 函数循环对象数组..

我试试:

 $test = str_replace($links, "file", "mnt");
 var_dump($test);

我得到了上面的错误。请问有什么想法吗?

【问题讨论】:

    标签: hyperlink phpexcel str-replace


    【解决方案1】:

    集合是一个对象数组,以单元格地址为索引; PHPExcel_Cell_Hyperlink 对象有一组记录在案的方法来访问和设置其数据:

    foreach($links as $cellAddress => $link) {
        // get the URL from the PHPExcel_Cell_Hyperlink object
        $url = $link->getUrl();
        // change the URL however you want here
        $url = str_replace($url, "file", "mnt");
        // Set the new value for the link
        $link->setUrl($url);
    }
    

    如果您只想修改 N 列中单元格的 URL,则可以将它们包装在 if 测试中:

    foreach($links as $cellAddress => $link) {
        // Test for column N
        sscanf($cellAddress, '%[A-Z]%d', $column, $row);
        if ($column == 'N') {
            // get the URL from the PHPExcel_Cell_Hyperlink object
            $url = $link->getUrl();
            // change the URL however you want here
            $url = str_replace($url, "file", "mnt");
            // Set the new value for the link
            $link->setUrl($url);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-26
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多