• 分享给大家一个简单的数据导出excel类


    <?php
    /**
     * 生成excel文件操作
     *
     * @author wesley wu
     * @date 2013.12.9
     */
    class Excel
    {
         
        private $limit = 10000;
         
        public function download($data, $fileName)
        {
            $fileName = $this->_charset($fileName);
            header("Content-Type: application/vnd.ms-excel; charset=gbk");
            header("Content-Disposition: inline; filename="" . $fileName . ".xls"");
            echo "<?xml version="1.0" encoding="gbk"?>
    
                <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
                xmlns:x="urn:schemas-microsoft-com:office:excel"
                xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
                xmlns:html="http://www.w3.org/TR/REC-html40">";
            echo "
    <Worksheet ss:Name="" . $fileName . "">
    <Table>
    ";
            $guard = 0;
            foreach($data as $v)
            {
                $guard++;
                if($guard==$this->limit)
                {
                    ob_flush();
                    flush();
                    $guard = 0;
                }
                echo $this->_addRow($this->_charset($v));
            }
            echo "</Table>
    </Worksheet>
    </Workbook>";
        }
         
        private function _addRow($row)
        {
            $cells = "";
            foreach ($row as $k => $v)
            {
                $cells .= "<Cell><Data ss:Type="String">" . $v . "</Data></Cell>
    ";
            }
            return "<Row>
    " . $cells . "</Row>
    ";
        }
         
        private function _charset($data)
        {
            if(!$data)
            {
                return false;
            }
            if(is_array($data))
            {
                foreach($data as $k=>$v)
                {
                    $data[$k] = $this->_charset($v);
                }
                return $data;
            }
            return iconv('utf-8', 'gbk', $data);
        }
         
    }
    

    //使用方法

    $excel = new Excel();
    
    $data = array(
        array('姓名','标题','文章','价格','数据5','数据6','数据7'),
        array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
        array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
        array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
        array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
        array('数据1','数据2','数据3','数据4','数据5','数据6','数据7')
    );
    
    $excel->download($data, '这是一个测试');
    
    ?>
    
  • 相关阅读:
    Python 元类
    Rsync 基础配置
    linux shell find
    找最大的目录
    云主机的上下行带宽
    关于c3p0 ResourcePoolException: Attempted to use a closed or broken resource pool
    recover_file
    MegaCli 监控raid状态
    influxdb
    在Ubuntu 16.04如何安装Java使用apt-get的
  • 原文地址:https://www.cnblogs.com/yzycoder/p/4741149.html
Copyright © 2020-2023  润新知