Excel 表 导入导出

//导出
public function exports($ids = [],$action='HP'){
switch ($action){
case 'HP':
$comment = ['会员卡卡号','余额数量','会员卡类型','备注'];//第一行标题内容
$field = ['cardnums', 'price_num', 'statustype','remark'];//第二行列字段内容
if(!empty($ids)){//查询指定的几条数据
$res = db('membercardgroup')
->field($field)
->where(['id'=>['in',$ids]])
->select();
foreach($res as &$re){
if($re['statustype']==0){
$re['statustype'] = '金额卡';
}else{
$re['statustype'] = '整箱卡';
}
}
}else{//查询所有数据
$res =db('membercardgroup')
->field($field)
// ->limit(5)
->select();
foreach($res as &$re){
if($re['statustype']==0){
$re['statustype'] = '金额卡';
}else{
$re['statustype'] = '整箱卡';
}
}
// echo '<pre/>';print_r($res);die;
}
break;
default:
$this->displayByError('行动有误,撤退!!');
}
$strTable ='<table width="500" border="1">';
$strTable .= '<tr>';
//第一行的标题
foreach ($comment as $key => $value){
$strTable .= '<td style="text-align:left;font-size:16px;" width="*">'.$value.'</td>'." ";
}
$strTable .= '</tr>';
 
//第二行的列字段
$strTable .= '<tr>';
foreach ($field as $key => $value){
$strTable .= '<td style="text-align:left;font-size:14px;" width="*">'.$value.'</td>'." ";
}
$strTable .= '</tr>';
 
//第三行开始是导入数据库的数据
$strTable .= '<tr>';
for ($i = 0;$i < count($res);$i++){
for ($j = 0; $j < count($field);$j++){
$strTable .= '<td style="text-align:left;font-size:12px;" rowspan="*">'.$res[$i][$field[$j]].'</td>'." ";
}
$strTable .= '</tr>';
}
$strTable .='</table>';
unset($res);
$this->downloadExcel($strTable,'GoodsExcel');
exit();
 
}
 
/**
* 导出excel需要设置的header
* @param $strTable /表格内容
* @param $filename /文件名
*/
public function downloadExcel($strTable,$filename)
{
header("Content-type: application/vnd.ms-excel");//将查询结果导出到Excel
header("Content-Type: application/force-download");//告诉浏览器强制下载
header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d H-i-s',time()).".xls");//attachment作为附件下载,inline在线下载,filename设置文件名
header('Expires:0');//浏览器不会响应缓存
header('Pragma:public');//Public指示响应可被任何缓存区缓存。
echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
}
 
 
 
 
//导入
public function upload()
{
require_once '/www/wwwroot/www/extend/PHPExcel/Classes/PHPExcel/IOFactory.php';//导入PHPExcel文件中的IOFactory.php类
// echo 111;die;
$file = request()->file('file');//获取文件,file是请求的参数名
$info = $file->move(ROOT_PATH . 'public' . DS . 'excel');//move将文件移动到项目文件的xxx
// echo '<pre/>';print_r(__FILE__);die;
if($info){
libxml_use_internal_errors(true);
 
$excel_path = $info->getSaveName(); //获取上传文件名
// $excel_suffix = $info->getExtension(); //获取上传文件后缀
$file_name = './../public' . DS . 'excel' . DS . $excel_path; //上传文件的地址
 
$obj_PHPExcel = PHPExcel_IOFactory::load($file_name); //加载文件内容
// echo '<pre/>';print_r($obj_PHPExcel);die;
$excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
$arr = reset($excel_array); //获取字段名
unset($excel_array[0]); //删除字段名,剩下的都是存储到数据库的数据了!!
$data = [];
for($i = 0;$i < count($excel_array);$i++){
foreach ($arr as $key => $value){
$data[$i][$value] = $excel_array[$i+1][$key];//使数组的键值就是数据表的字段名
}
}
foreach($data as &$v){
if($v['statustype']=='金额卡'){
$ins['statustype'] = 0;
}elseif($v['statustype']=='整箱卡'){
$ins['statustype'] = 1;
}else{
continue;
}
 
$ins['title'] = $v['title'];
$ins['cardnums'] = $v['cardnums'];
$ins['code'] = $v['code'];
$ins['price_num'] = $v['price_num'];
$ins['image'] = $v['image'];
$ins['status'] = $v['status'];
$ins['addtime'] = time();
// echo '<pre/>';print_r($ins);die;
$resint = db('membercardgroup')->insert($ins);
}
 
return json(['code'=>1,'msg'=>'成功','url'=>'https://'.$_SERVER['HTTP_HOST'].'/public/excel/' . $excel_path]);
}
}

原文地址:https://www.cnblogs.com/DevilBoy/p/14087641.html