zoukankan      html  css  js  c++  java
  • php 导出

    //导出
    //放在model层的类
    <?phpnamespace frontendmodels;
    use yiiasemodel;
    /**
    * @copyright (c) 2014 aircheng
    * @file report.php
    * @brief 导出excel类库
    * @author dabao
    * @date 2014/11/28 22:09:43
    * @version 1.0.0
    * @update 4.6
    * @date 2016/9/15 23:30:28
    * @author nswe
    * @content 重构了写入方式和方法
    */
    class Excal extends Model
    {
       //文件名
      private $fileName = 'user';
      //数据内容
      private $_data    = "";
      //构造函数
      public function __construct($fileName = '')
     {
      $this->setFileName($fileName);
     }
    //设置要导出的文件名
     public function setFileName($fileName)
    {
    $this->fileName = $fileName;
    }
     /**
     * @brief 写入内容操作,每次存入一行
     * @param $data array 一维数组
     */
     public function setTitle($data = array())
     {
    array_walk($data,function(&$val,$key)
     {
      $val = "<th style='text-align:center;color:#fff;font-size:12px;vnd.ms-excel.numberformat:@'>".$val."</th>";
     });
    $this->_data .= "<tr>".join($data)."</tr>";
    }
    /**
    * @brief 写入标题操作
    * @param $data array  数据
    */
    public function setData($data = array())
    {
     array_walk($data,function(&$val,$key)
     {
    $val = "<td style='text-align:center;font-size:12px;vnd.ms-excel.numberformat:@'>".$val."</td>";
    });
    $this->_data .= "<tr>".join($data)."</tr>";
    }
     //开始下载
    public function toDownload($data = '')
    {
    // Redirect output to a client’s web browser (Excel5)
     header('Content-Type: application/vnd.ms-excel');
     header('Content-Disposition: attachment;filename='.$this->fileName.'_'.date('Y-m-d').'.xls');
     header('Cache-Control: max-age=0');
     // If you're serving to IE 9, then the following may be needed
     header('Cache-Control: max-age=1');
     // If you're serving to IE over SSL, then the following may be needed
     header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
     header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
     header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
     header ('Pragma: public'); // HTTP/1.0
    $result = $data ? $data : "<table border='1'>".$this->_data."</table>";
     echo <<< OEF
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <body>
    {$result}
    </body>
    </html>
    OEF;
    }}
    
    //把类放到model层之后 调用类开始导出
    //导出excel
    
      public function actionExcel()
      {
            $sql="select * from shop";
            $excel=Yii::$app->db->createCommand($sql)->queryAll();
            //导出excel
            $reportObj =new Excal('hotel');
            $reportObj->setTitle(["ID","标题","简介","时间"]);
            foreach($excel as $k=>$v)
            {
                $insertData=array($v['s_id'],$v['title'],$v['desc'],$v['time']);
                $reportObj->setData($insertData);
            }
            $reportObj->toDownload();
        }
    //导出word
        public function actionWord()
        {
            $sql = "select * from shop";
            $data = Yii::$app->db->createCommand($sql)->queryAll();
            header("content-type:text/html;charset=utf-8");
            header ( 'Content-Disposition: attachment; filename="word.doc"' );
            echo iconv("utf-8","gb2312","编号")."	";
            echo iconv("utf-8","gb2312","文章主题")."	";
            echo iconv("utf-8","gb2312","壁纸")."	";
            echo iconv("utf-8","gb2312","发表人")."	";
            echo iconv("utf-8","gb2312","时间")."	";
            echo iconv("utf-8","gb2312","文章内容")."
    ";
    
            foreach ($data as $key => $val)
            {
                echo iconv("utf-8","gb2312","$val[s_id]")."	";
                echo iconv("utf-8","gb2312","$val[title]")."	";
                echo iconv("utf-8","gb2312","$val[desc]")."	";
                echo iconv("utf-8","gb2312","$val[time]")."
    ";
            }
    
        }
    //实时保存
        public function actionKeep()
        {
    
            $data=Yii::$app->request->post();
            //print_r($data);die;
            $cookie=new yiiwebcookie();
            $cookie->name='goods';             //cookie的值
            $cookie->expire=time()+3600;        //存活的时间
            $cookie->value=$data;                //cookie的值
            Yii::$app->response->getCookies()->add($cookie);
        }
        //快速添加txt
        public function actionKuai()
        {
            $txt = $_FILES['txt']['tmp_name'];
            $str = file_get_contents("$txt");//将整个文件内容读入到一个字符串中
            $str_encoding = mb_convert_encoding($str, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');//转换字符集(编码)
            $arr = explode("
    ", $str_encoding);//转换成数组
        //去除值中的空格
            foreach ($arr as &$row)
            {
                $row = trim($row);
            }
            unset($row);
            $time = time();
            //得到后的数组
            $info = Yii::$app->db->createCommand()->insert('shop',
                array('title' => $arr[0],'desc' =>$arr[1],'time' =>$time))->execute();
            if ($info)
            {
                return $this->redirect('?r=shop/shop');
    
            } else {
                return $this->redirect('?r=shop/adds');
            }
    
        }
  • 相关阅读:
    linux-CPU和核概念
    Nginx学习——location+proxy_pass左斜杠问题彻底弄清
    es概念一句话简介和注意点
    es-字段类型整理(6.x及以上)
    Nginx学习——location和rewrite
    Nginx学习——proxy_pass
    Nginx学习——简介及常用命令
    第三方接口调用异常补偿机制实现实例记录
    postgres日志爆盘处理方案-转自DBA汪x
    Xshell连接本地 Virtualbo Ubuntu
  • 原文地址:https://www.cnblogs.com/zhangtianle/p/7365384.html
Copyright © 2011-2022 走看看