php导出excel_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > php导出excel

php导出excel

 2012/12/16 17:23:07  Lamp兄弟  程序员俱乐部  我要评论(0)
  • 摘要:/***@paramstring$filname//输出的文件名*@paramstring$type//编码类型*@paramstring$array//excel的标题*@paramstring$data//需要的数据data**/functionexportExcel($filname,$type='utf-8',$array,$data){if(empty($data)){echo"<script>alert('sorry,nodata!');history.go(-1)
  • 标签:PHP excel 导出excel
  /**
     * @param string $filname             //输出的文件名
     * @param string $type                //编码类型
     * @param string $array              //excel的标题
     * @param string $data               //需要的数据data
     **/
    function exportExcel($filname, $type = 'utf-8', $array, $data) {
        if (empty ($data)) {
            echo "<script>alert('sorry,no data!');history.go(-1);</script>";
            exit;
        }
        $filname = iconv("utf-8", "gb2312", $filname);
        header("Content-type:application/vnd.ms-excel;");
        header("Content-Disposition:attachment;filename=$filname.xls");
        echo "<META HTTP-EQUIV='Content-Type' CONTENT='text/html;charset=$type'>";
        echo '<table border="1" cellspacing="1" cellpadding="1"><tr align="center">';
        foreach ($array as $val) {
            echo "<td width='100'>$val</td>";
        }
        echo '</tr>';
        foreach ($data as $val) {
            if(is_array($val)){
                echo '<tr align="center">';
                foreach ($val as $v) {
                echo '<td width="120" height="30">' . $v . '</td>';
                }
                echo '</td>';
            }else {
                echo '<tr align="center">';
                echo '<td width="120" height="30">' . $val . '</td>';
                echo '</td>';
            }
        }
        echo '</table>';
        exit;
    }
 
发表评论
用户名: 匿名