zoukankan      html  css  js  c++  java
  • php导出excel表格超链接

    //导出的数据源 二维数组
                $data = [
                    ['name' => '1','phone' =>'1的电话','pic_url' =>[]],
                    ['name' => '2','phone' =>'2的电话','pic_url' =>[]]
                ];
                //设置参数
                $indexKey = ['name', 'phone', 'pic_url'];//与表头对应的要导出的二维数组的键
                $thValue = ["姓名", '电话', '图片'];//表头
                $fileName = "测试导出表";//表名
                $table = '';
                //需要特殊处理的单元格,对应二维数组的键
                $urlArr = ['pic_url'];
                //表头
                $table .= "<table border='1' cellspacing='0' cellpadding='0'><thead><tr>";
                foreach($thValue as $v) {
                    $table .= "<th> ".$v."</th >";
                }
                $table .= "</tr></thead><tbody>";
                //主体
                foreach ($data as $value) {
                    $table .= "<tr align='left'>";
                    foreach ($indexKey as $val) {
                        if (in_array($val,$urlArr)) {
                            //处理图片单元格 且合并到一格
                            $table .= "<td width='800'>";
                            //示例的链接为数组,如果不是数组将遍历去除即可
                            foreach ($value[$val] as $v_url) {
                                $table .= "<div style=' 800px;'><a href='". $v_url ."'>" . $v_url . "</a></div>";
                            }
                            $table .= "</td>";
                        } else {
                            //普通单元格 内容前面留一个空格,防止长数字被格式化
                            $table .= "<td width='150'> " . $value[$val] . "</td>";
                        }
                    }
                    $table .= "</tr>";
                }
                //表结尾
                $table .= "</tbody></table>";
                //通过header头控制输出excel表格
                header("Pragma: public");
                header("Expires: 0");
                header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
                header("Content-Type:application/force-download");
                header("Content-Type:application/vnd.ms-execl");
                header("Content-Type:application/octet-stream");
                header("Content-Type:application/download");
                header('Content-Disposition:attachment;filename="'.$fileName.'.xls"');
                header("Content-Transfer-Encoding:binary");
                echo $table;
  • 相关阅读:
    12.数组三--数组的冒泡排序与快速排序
    11.数组二
    10.数组一
    Vue之组件与父子传值
    Django模型层
    面向对象的组合用法
    面向对象初识
    Python内置函数
    列表推导式,生成器表达式
    装饰器进阶
  • 原文地址:https://www.cnblogs.com/zwjphp/p/15156395.html
Copyright © 2011-2022 走看看