php 导出
Posted 醉里浪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 导出相关的知识,希望对你有一定的参考价值。
//导出
//放在model层的类
<?phpnamespace frontend\models;
use yii\base\model;
/**
* @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","编号")."\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‘);
}
}
以上是关于php 导出的主要内容,如果未能解决你的问题,请参考以下文章
php 导出excel 这种方法宽度无法设置 只能自动适应?