导出表格
Posted ysboke
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导出表格相关的知识,希望对你有一定的参考价值。
base中
/**
* [phpexcel excel导出!不支持ajax js用window.open]
* @Author aaron_yw
* @DateTime 2018-12-14T17:08:04+0800
* @copyright [copyright]
* @license [license]
* @version [version]
* @param [一维数组] $title [标题行名称]
* @param [必须为二维数组] $data [导出数据]
* @param [字符串] $topic [文件名称]
* @return [type] [description]
*/
protected function phpexcel($title,$data,$topic){
//3.实例化PHPExcel类
$objPHPExcel = new PHPExcel();
//4.激活当前的sheet表
$cellName = array(‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘, ‘AA‘, ‘AB‘, ‘AC‘, ‘AD‘, ‘AE‘, ‘AF‘, ‘AG‘, ‘AH‘, ‘AI‘, ‘AJ‘, ‘AK‘, ‘AL‘, ‘AM‘, ‘AN‘, ‘AO‘, ‘AP‘, ‘AQ‘, ‘AR‘, ‘AS‘, ‘AT‘, ‘AU‘, ‘AV‘, ‘AW‘, ‘AX‘, ‘AY‘, ‘AZ‘);
$objPHPExcel->getActiveSheet(0)->setTitle(‘sheet名称‘); //设置sheet名称
$_row = 1; //设置纵向单元格标识
if($title){
$_cnt = count($title);
$objPHPExcel->getActiveSheet(0)->mergeCells(‘A‘.$_row.‘:‘.$cellName[$_cnt-1].$_row); //第一行合并单元格
$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A‘.$_row, $topic.date(‘Y-m-d H:i:s‘));//第一行合并后的单元格内容
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setSize(16);//第一行字体大小
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//第一行水平居中
$_row++;
$i = 0;
foreach($title AS $v){
//5.设置列标题
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].$_row, $v);
$i++;
}
$_row++;
}
//6.填写数据
if($data){
$i = 0;
foreach($data AS $_v){
$j = 0;
foreach($_v AS $_cell){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i+$_row), $_cell);
$j++;
}
$i++;
}
}
//7.设置保存的Excel表格名称
$filename = $topic.date(‘y-m-d H:i:s‘,time()).‘.xls‘;
//8.设置当前激活的sheet表格名称;
$objPHPExcel->getActiveSheet()->setTitle($topic);
//9.设置浏览器窗口下载表格
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header(‘Content-Disposition:inline;filename="‘.$filename.‘"‘);
//生成excel文件
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);
//下载文件在浏览器窗口
$objWriter->save(‘php://output‘);
exit;
}
按钮
<a href="javascript:;" onclick="daochu(this)" class="btn btn-danger radius"><i class="Hui-iconfont"></i> 导出</a>
方法
//-----------导出----------
function daochu(obj,type){
//在这里面输入任何合法的js语句
layer.open({
type: 1 //Page层类型
,area : [‘350px‘,‘200px‘]
,title: ‘请填入您要导出的数据条数‘
,shade: 0.6 //遮罩透明度
,shadeClose:true
,maxmin: false //允许全屏最小化
,anim: 1 //0-6的动画形式,-1不开启
,content: ‘<div class="layui-layer-content"><input id="sjnum" placeholder="您所填的必须是整数" style="width:200px;height:30px;margin:20px 70px; text-align:center;" class="layui-layer-input" value=""><span style="padding-left:88px;">导出顺序为用户编号正序</span></div>‘
,btn: [‘确定‘, ‘取消‘]
,success: function(index, layero){
}
,yes: function(index, layero){
var num = $("#sjnum").val();
var objExp = /^+?[1-9][0-9]*$/;
if(!objExp.test(num)){
layer.msg(‘请填入非零的正整数‘,{icon:4,shade:0.01,time:3000});
$("#sjnum").val(‘‘);
return false;
}
window.open("{:url(‘User/daochu‘)}?num="+num);
location.reload();
}
,btn2 : function (index, layero) {
}
});
}
控制器
public function daochu()
{
$num = input("num");
$data = Db::name(‘user‘)->limit($num)->select();
foreach ($data as $key => $val) {
$data[$key][‘time‘] = $this->showTime($val[‘time‘]);
}
$title = array("编号","登录名","密码","加入时间","状态","登陆次数","登录时间","ip地址");
$this->phpexcel($title, $data,"管理人员表");
}
cmd运行
composer
require
phpoffice/phpexcel
以上是关于导出表格的主要内容,如果未能解决你的问题,请参考以下文章