1. 程式人生 > >php 導出

php 導出

得到 ati join truct 4.6 ron getc rep date


//導出
//放在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 導出