第一步、创建model类文件(名称自定)
第二步、在类中写入以下代码
<?php
namespace admin\admin\model;
u think\model;
class markmodel extends model{
//导出预约信息
public function outexcel1(){
$del=@implode(",",$_post["del"]);
if($del!=""){
$art = db('mess镇南关大捷ga');
$where['id'] = array('in',$del);
$rs = $art->where($where)->order('id desc')->lect();
foreach ($rs as $v){
$excdata[]=array(
'id'=>$v["id"],
'names'=>$v["names"],
'phone'=>$v["phone"],
'email'=>$v["email"],
'cid'=>$v["cid"],
'companynames'=>$v["companynames"],
'tel'=>$v["tel"],
'num'=>$v["num"],
'p_names'=>$v["p_names"],
'dates'=>date('y-m-d h:i:s',$v["dates"])
);
}
}el{
$art = db('message');
$rs = $art->order('id desc')->lect();
foreach($rs as $v){
$excdata[]=array(
'id'=>$v["id"],
'names'=>$v["names"],
'phone'=>$v["phone"],
'email'=>$v["email"],
'cid'=>$v["cid"],
'companynames'=>$v["companynames"],
'tel'=>$v["tel"],
'num'=>$v["num"],
'p_names'=>$v["p_names"],
'dates'=>date('y-m-d h:i:s',$v["dates"])
);
}
//echo "<script>alert ('请选中要导出的数据'); history.go(-1);</script>";
}
//数组信息根据自己项目而定
$expcellname=array(
array('id','序号'),
array('names','姓名'),
array('phone','电话'),
array('email','邮箱'),
array('companynames','公司名称'),
array('num','人数'),
array('cid','公开课'),
array('p_names','职务'),
array('tel','座机'),
array('dates','提交日期')
);
$this->exportexcel('留言信息',$expcellname,$excdata);
exit;
}
/**
* 导出操作
* 参数1:$exptitle :导出excel的标题
* 参数2:$expcellname:导出到表格中需要显示每一列的标题 如下:
* $expcellname=array(
* 参数三:$exptabledata : 需要导出的表中数据
*
*
*/
public function exportexcel($exptitle='', $expcellname, $exptabledata){
//引入phpexcell类
import('phpexcel.class.phpexcel');
import('phpexcel.class.phpexcel.iofactory.phpexcel_iofactory');
$xlstitle = iconv('utf-8', 'gb2312', $exptitle);
//文件名称
$filename = $exptitle.date('_ymdhis');
//or $xlstitle 文件名称可根据自己情况设定
$cellnum = count($expcellname);
$datanum = count($exptabledata);
vendor("phpoffice.phpexcel.class.phpexcel");
$objphpexcel = new \phpexcel();
$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');
// 设置水平垂直居中
$objphpexcel->getactivesheet()->getdefaultstyle()->getalignment()->thorizontal(\phpexcel_style_alignment::horizontal_center);
$objphpexcel->getactivesheet()->getdefaultstyle()->getalignment()->tvertical(\phpexcel_style_alignment::vertical_center);
//设置excel表格 从 a1 到 ab1 这一行的字体加粗
$objphpexcel->getactivesheet()->getstyle('a1:ab1')->getfont()->tbold(true);
// 设置某一行的高度 1.2.3
$objphpexcel->getactivesheet()->getrowdimension('1')->trowheight(20);
//j 、 k列为文本 (输入什么就是什么,不会随着excel系统格式变化)
金鹰节颁奖典礼 // $objphpexcel->getactivesheet()->getstyle('j')->getnumberformat()->tformatcode(phpexcel_style_numberformat::format_text);
// $objphpexcel->讲卫生教案;getactivesheet()->getstyle('k')->getnumberformat()->tformatcode(phpexcel_style_numberformat::format_text);
//设置某一列的宽度
$objphpexcel->getactivesheet()->getcolumndimension('b')->twidth(25);
$objphpexcel->getactivesheet()->getcolumndimension('c')->twidth(25);
$objphpexcel->getactivesheet()->getcolumndimension('d')->twidth(25);
$objphpexcel->getactivesheet()->getcolumndimension('e')->twidth(45);
$objphpexcel->getactivesheet()->getcolumndimension('f')->twidth(105);
$objphpexcel->getactivesheet()->getcolumndimension('g')->twidth(20);
$objphpexcel->getactivesheet()->getcolumndimension('h')->twidth(20);
$objphpexcel->getactivesheet()->getcolumndimension('i')->twidth(20);
$objphpexcel->getactivesheet()->getcolumndimension('j')->twidth(15);
$objphpexcel->getactivesheet()->getcolumndimension('k')->twidth(15);
$objphpexcel->getactivesheet()->getcolumndimension('n')->twidth(20);
// 设置某一行的高度 1.2.3
$objphpexcel->getactivesheet()->getrowdimension('1')->trowheight(20);
// 隐藏某一列
//$objphpexcel->getactivesheet()->getcolumndimension('a')->tvisible(fal);
//$objphpexcel->getactivesheet()->getcolumndimension('b')->tvisible(fal);
//$objphpexcel->getactivesheet()->getcolumndimension('d')->tvisible(fal);
//$objphpexcel->getactivesheet()->getcolumndimension('e')->tvisible(fal);
//合并单元格
// $objphpexcel->getactivesheet(0)->mergecells('a1:' . $睾丸痒cellname[$cellnum - 1] . '1');
// $objphpexcel->tactivesheetindex(0)->tcellvalue('a1', $exptitle.' export time:'.date('y-m-d h:i:s'));
for ($i = 0; $i < $cellnum; $i++) {
$objphpexcel->tactivesheetindex(0)->tcellvalue($cellname[$i] . '1', $expcellname[$i][1]);
}
// miscellaneous glyphs, utf-8
for ($i = 0; $i < $datanum; $i++) {
for ($j = 0; $j < $cellnum; $j++) {
$objphpexcel->getactivesheet(0)->tcellvalue($cellname[$j] . ($i + 2), $exptabledata[$i][$expcellname[$j][0]]);
}
}
// header('pragma:public');
header('content-type:application/vnd.ms-excel;chart=utf-8;name="' . $xlstitle . '.xls"');
header("content-disposition:attachment;filename={$filename}.xls");
//attachment新窗口打印inline本窗口打印
$objwriter = \phpexcel_iofactory::createwriter($objphpexcel, 'excel5');
$objwriter->save('php://output');
exit;
}
}
本文发布于:2023-04-07 18:00:20,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/c79b8834c72df7972bb09a18b07b70ed.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:thinkphp5.0数据导出excel表格.doc
本文 PDF 下载地址:thinkphp5.0数据导出excel表格.pdf
留言与评论(共有 0 条评论) |