博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php 导出
阅读量:5263 次
发布时间:2019-06-14

本文共 3824 字,大约阅读时间需要 12 分钟。

//导出//放在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 : "
".$this->_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'); } }

 

转载于:https://www.cnblogs.com/zhangtianle/p/7365384.html

你可能感兴趣的文章
go:channel(未完)
查看>>
[JS]递归对象或数组
查看>>
LeetCode(17) - Letter Combinations of a Phone Number
查看>>
Linux查找命令对比(find、locate、whereis、which、type、grep)
查看>>
路由器外接硬盘做nas可行吗?
查看>>
python:从迭代器,到生成器,再到协程的示例代码
查看>>
Java多线程系列——原子类的实现(CAS算法)
查看>>
在Ubuntu下配置Apache多域名服务器
查看>>
多线程《三》进程与线程的区别
查看>>
linux sed命令
查看>>
html标签的嵌套规则
查看>>
[Source] Machine Learning Gathering/Surveys
查看>>
HTML <select> 标签
查看>>
类加载机制
查看>>
tju 1782. The jackpot
查看>>
湖南多校对抗赛(2015.03.28) H SG Value
查看>>
hdu1255扫描线计算覆盖两次面积
查看>>
hdu1565 用搜索代替枚举找可能状态或者轮廓线解(较优),参考poj2411
查看>>
bzoj3224 splay板子
查看>>
程序存储问题
查看>>