晦涩de咚 2016-01-13 14:31:23 8650次浏览 9条评论 14 0 0
class DownLoadExcel
{

    private $limit = 10000;

    public function download($data, $fileName)
    {
        $fileType = explode('.',$fileName)[1];
        $fileName = $this->_charset($fileName);
        if( $fileType == 'xls' || $fileType == 'xlsx')
        {
            header("Content-Type: application/vnd.ms-excel; charset=GB2312");
            header("Content-Disposition: attachment; filename=\"" . $fileName. "\"");
            $str = '';
            foreach ($data as $row) {
                $str_arr = array();
                foreach ($row as $column) {
                    $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
                }
                $str.=implode(chr(9), $str_arr) . PHP_EOL;
            }
            echo $this->_charset($str);
        }else if($fileType == 'csv')
        {
            header("Content-Type: application/vnd.ms-excel; charset=GB2312");
            header("Content-Disposition: attachment;filename=".$fileName);
            $str = '';
            foreach ($data as $row) {
                $str_arr = array();
                foreach ($row as $column) {
                    $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
                }
                $str.=implode(',', $str_arr) . PHP_EOL;
            }
            echo $this->_charset($str);
        }else
        {
            exit("Not supported".$fileType);
        }

    }

    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', 'GB2312', $data);//utf-8转化为gbk
    }

}

//使用例子:

$data = array(
    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')
);
$execl = new DownLoadExcel();
$execl->download($data,'test.xls');
  • 评论于 2016-01-15 17:13 举报

    不好意思,新人~能教一下怎样使用吗?

    5 条回复
    评论于 2016-01-18 08:50 回复

    下面不是有使用例子吗,只需要传一个如例子中的$data数组和文件名与其后缀。

    评论于 2016-01-18 17:11 回复

    我直接使用例子,在页面有显示~但是没有下载成excel文件

    评论于 2016-01-19 17:31 回复

    倪代马怎么写的?这写代码放在一个页面吗?你有看运行的时候报错没?

    评论于 2016-03-17 13:27 回复
    评论于 2016-03-21 15:08 回复

    @晦涩de咚收到

  • 评论于 2016-01-21 11:03 举报

    代码添加“namespace common\extensions;”,保存,然后将class文件DownLoadExcel.php放在common\extensions下,
    在一个控制文件里添加如下action,然后访问即可

    public function actionReadexcel()
    {
        $data = array(
            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')
        );
    
        $execl = new \common\extensions\DownLoadExcel();
        $execl->download($data,'test.xls');
    }
    
    1 条回复
    评论于 2016-01-21 11:53 回复

    yii 2,你应该在控制器里面 $execl = new DownLoadExcel(); 应该会自动加上use common\extensions吧。这样好看些吧。

  • 评论于 2016-03-02 17:59 举报

    赞。。。。。。。。。。

  • 评论于 2016-04-25 09:48 举报
     请问一下   写好了以后再本地导出正常  在服务器上导出    导出的格式不正确
    
    2 条回复
    评论于 2016-04-25 17:40 回复

    格式变成啥样了? 好像这个不能用正版的office打开,用wps可以。

    评论于 2016-04-26 18:09 回复

    丢到服务器上就不对了 然后我用phpExcel写的

  • 评论于 2016-05-19 15:48 举报

    可以可以,亲测可行

  • 评论于 2016-05-26 09:14 举报

    大哥,有word吗

    4 条回复
    评论于 2016-05-26 13:50 回复

    兄弟,你不妨试一试 http://demos.krajee.com/grid#grid-export ;个人觉得这个结合的比较好

    评论于 2016-05-26 14:25 回复

    这能整出word?

    评论于 2016-05-26 14:37 回复

    @luanzhiyong 不能,但是能够pdf等;:(html, csv, txt, xls, pdf, json).

    评论于 2016-05-26 15:18 回复

    大哥,知不知道phpword咋在yii2中使

  • 评论于 2016-10-19 17:54 举报

    DownLoadExcel 类文件 放在哪个目录里

    1 条回复
    评论于 2016-10-20 08:39 回复

    根据个人开发习惯,可以学习上面这个评论的方法: heenji heenji 评论于 2016-01-21 11:03

  • 评论于 2016-10-19 18:30 举报

    如何自定义增加 导出内容的每列标题呢

    1 条回复
    评论于 2016-10-20 08:40 回复

    $data是可以自定义重组的,你处理下,不就ok

    觉得很赞
  • 评论于 2016-10-25 10:57 举报

    楼主修改下download,导出数据量过大会溢出问题的解决

    public function download($data, $fileName)
        {
    
            $fileType = explode('.',$fileName)[1];
            $fileName = $this->_charset($fileName);
            if( $fileType == 'xls' || $fileType == 'xlsx')
            {
                header("Content-Type: application/vnd.ms-excel; charset=GB2312");
                header("Content-Disposition: attachment; filename=\"" . $fileName. "\"");
                $str = '';
                $guard = 0;
                foreach ($data as $row) {
                    $guard++;
                    if($guard==$this->limit)
                    {
                        ob_flush();
                        flush();
                        $guard = 0;
                    }
                    $str_arr = array();
                    foreach ($row as $column) {
                        $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
                    }
                    $str.=$this->_charset(implode(chr(9), $str_arr) . PHP_EOL);
                }
                echo $str;
            }else if($fileType == 'csv')
            {
                header("Content-Type: application/vnd.ms-excel; charset=GB2312");
                header("Content-Disposition: attachment;filename=".$fileName);
                $str = '';
                $guard = 0;
                foreach ($data as $row) {
                    $guard++;
                    if($guard==$this->limit)
                    {
                        ob_flush();
                        flush();
                        $guard = 0;
                    }
                    $str_arr = array();
                    foreach ($row as $column) {
                        $str_arr[] = '"' . str_replace('"', '""', $column) . '"';
                    }
                    $str.=$this->_charset(implode(',', $str_arr) . PHP_EOL);
                }
                echo $str;
            }else
            {
                exit("Not supported".$fileType);
            }
    
    
您需要登录后才可以评论。登录 | 立即注册