分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

Thinkphp excel批量导出-分表导出

发布时间:2023-09-06 01:34责任编辑:苏小强关键词:excel

前天有一个兄弟问我能不能将数据库里的数据,批量,分表,设定每个表有多少条,这么将数据导出来,想了想。觉得是可以实现的,想了想简单的写了个代码,也遇到了困难,就是“设定每个表多少条”数据的时候出现了问题,最后折中的一个办法。

做了一个前端提交每个表多少条数据的,可以进行设定参数的。

实验数据5000条

前端代码

 <!DOCTYPE html> <html> <head>  <meta charset="UTF-8">  <title>Document</title> </head> <body>  <p>总共含有:{$num}条数据</p>  <form action="{:U(‘Index/index‘)}" method="post">  <input type="text" name="num" placeholder="每表条数">  <input type="submit" >  </form>  <p></p> </body> </html>

后台代码

<?phpnamespace Home\Controller;use Think\Controller;class IndexController extends Controller {//添加数据    public function index(){    $count = D("User")->count();    if(IS_POST){    $num = I(‘post.num‘);    $grade = $count/$num+1;    for($i=1;$i<$grade;$i++){    $url = "http://127.0.0.1/index.php/Home/Index/demo?strat=".$i."&end=".$num;echo "<script>window.open(‘$url‘)</script>";    }    echo "完成";    }else{    $this->assign("num",$count);    $this->display();        }    }    //批量导出数据public function demo(){$strat = I(‘get.strat‘);$end = I(‘get.end‘);$ResultInfo = D(‘User‘)->page("$strat,$end")->select();    // 清空(擦除)缓冲区并关闭输出缓冲    ob_end_clean();    //引入PHPExcel库文件    import("Org.Util.PHPExcel");    //创建对象    $excel = new \PHPExcel();    $excel->getActiveSheet()->setTitle(‘投诉列表‘);    // 设置单元格高度    // 所有单元格默认高度    $excel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(25);    // 第一行的默认高度    $excel->getActiveSheet()->getRowDimension(‘1‘)->setRowHeight(30);    // 垂直居中    $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);    // 设置水平居中    $excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);    //Excel表格式    $letter = array(‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘F‘,‘G‘,‘H‘,‘I‘,‘Z‘,‘K‘);    //设置表头    $tableheader = array(‘序号‘,‘插入时间‘,‘修改时间‘,‘数据状态‘,‘姓名‘,‘密码‘,‘手机号‘,‘性别‘,‘年龄‘,‘邮箱‘,‘地址‘);    //设置表头表格宽度    $tablestyle = array(        array(‘width‘=>‘5‘),    //序号        array(‘width‘=>‘15‘),   //插入时间        array(‘width‘=>‘15‘),   //修改时间        array(‘width‘=>‘15‘),   //数据状态        array(‘width‘=>‘30‘),   //姓名        array(‘width‘=>‘10‘),   //密码        array(‘width‘=>‘20‘),   //密码        array(‘width‘=>‘20‘),   //手机号        array(‘width‘=>‘10‘),   //性别        array(‘width‘=>‘10‘),   //年龄        array(‘width‘=>‘80‘),   //邮箱        array(‘width‘=>‘10‘),   //地址    );    //填充表头信息    for($i = 0;$i < count($tableheader);$i++) {        $excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");        $excel->getActiveSheet()->getColumnDimension($letter[$i])->setWidth($tablestyle[$i][‘width‘]);    }    //填充表格信息    for ($i = 2;$i <= count($ResultInfo) + 1;$i++) {        $j = 0;        foreach ($ResultInfo[$i - 2] as $key=>$value) {            $excel->getActiveSheet()->setCellValue("$letter[$j]$i","$value");            $j++;        }    }    //将文件保存在服务器时,打开的浏览器窗口不能自动关闭    // $write = new \PHPExcel_Writer_Excel5($excel);    // $filename = "./Public/excel/".date(‘Y-m-d‘,time())."-".$strat.".xls";    // $write->save($filename);    //直接下载的代码    $write = new \PHPExcel_Writer_Excel5($excel);    $write->save($filename);    header("Pragma: public");    header("Expires: 0");    header("Expires: 0");    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");    header("Content-Type:application/force-download");    header("Content-Type:application/vnd.ms-execl;charset=utf-8");    header("Content-Type:application/octet-stream");    header("Content-Type:application/download");    header(‘Content-Disposition:attachment;filename=‘.date(‘Y-m-d‘)."-".$strat.‘.xls‘);    header("Content-Transfer-Encoding:binary");    $write->save(‘php://output‘);}}

不足之处,就是保存在本地的时候,下载完成的空白窗口不会自动关闭,这个暂时没有完美的解决。

关于thinkphp使用phpexcel批量导入和导出的,可以查看相关文章。

Thinkphp excel批量导出-分表导出

原文地址:https://www.cnblogs.com/shenlin/p/8228379.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved