当前位置 : 主页 > 网络编程 > PHP >

Excel.php

来源:互联网 收集:自由互联 发布时间:2021-06-28
Excel.php array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.
Excel.php
  array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
        'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
    );

    /**下载文件
     * @param string $filename 文件名称
     * @param string $data     数据字符串
     * @return bool
     */
    public static function force_download($filename = '', $data = '')
    {
        if ($filename == '' OR $data == '') {
            return FALSE;
        }
        if (FALSE === strpos($filename, '.')) {
            return FALSE;
        }
        // Grab the file extension
        $x = explode('.', $filename);
        $extension = end($x);
        $mime = (is_array(self::$mimes[$extension])) ? self::$mimes[$extension][0] : self::$mimes[$extension];
        // Generate the server headers
        header('Content-Type: "' . $mime . '"');
        header('Content-Disposition: attachment; filename="' . $filename . '"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header("Content-Transfer-Encoding: binary");
        header('Pragma: public');
        header("Content-Length: " . strlen($data));
        exit($data);
    }

    /**
     * 编码转换
     * @param type $string
     * @return string
     */
    public static function charset($string){
        return iconv('utf-8', 'gbk', $string);
    }

    /**
     * 格式化excel
     * @param $th_arr  th头部信息
     * @param $list_arr 表数据信息
     * @return string
     */
    public static function format_excel_data($th_arr, $list_arr)
    {
        $th_str = '';
        $tr_str = '';
        foreach ($th_arr as $key => $th) {
            $th_str .= '' . self::charset($th['name']) . '';
        }
        $th_all_keys    =    array_keys($th_arr['0']);
        $first_key_name = array_shift($th_all_keys);
        $last_key_name  = array_pop($th_all_keys);
        foreach($list_arr as $list)
        {
            foreach($list as $key=>$list_detail){
                if($key==$first_key_name){
                    $tr_str .= '';
                }
                $tr_str .= '' . self::charset($list_detail) . '';
                if($key==$last_key_name)
                {
                    $tr_str .= '';
                }
            }

        }
        $th_str .= '';



        $total_num = count($list_arr);
        $date_time = date('Y年m月d日 H:i:s');
        $total_str = <<
 
上一篇:basedao.php
下一篇:time.function.php
网友评论