//导出//放在model层的类 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 = "".$val.""; });$this->_data .= "".join($data)."";}/*** @brief 写入标题操作* @param $data array 数据*/public function setData($data = array()){ array_walk($data,function(&$val,$key) {$val = "".$val."";});$this->_data .= "".join($data)."";} //开始下载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 : "
"; echo <<< OEF
{
$result}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","编号")."\t"; echo iconv("utf-8","gb2312","文章主题")."\t"; echo iconv("utf-8","gb2312","壁纸")."\t"; echo iconv("utf-8","gb2312","发表人")."\t"; echo iconv("utf-8","gb2312","时间")."\t"; echo iconv("utf-8","gb2312","文章内容")."\n"; foreach ($data as $key => $val) { echo iconv("utf-8","gb2312","$val[s_id]")."\t"; echo iconv("utf-8","gb2312","$val[title]")."\t"; echo iconv("utf-8","gb2312","$val[desc]")."\t"; echo iconv("utf-8","gb2312","$val[time]")."\n"; } }//实时保存 public function actionKeep() { $data=Yii::$app->request->post(); //print_r($data);die; $cookie=new \yii\web\cookie(); $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("\r\n", $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'); } }