tp5导出execl
代码
<?php namespace app\system\controller; use app\common\model\Business; use PHPExcel_IOFactory; use PHPExcel; use think\Db; class User extends Common { /* * @param 导出excel */ public function phpdc(){ vendor("PHPExcel.PHPExcel"); vendor("PHPExcel.PHPExcel.Writer.Excel5"); vendor("PHPExcel.PHPExcel.Writer.Excel2007"); vendor("PHPExcel.PHPExcel.IOFactory"); $objPHPExcel = new \PHPExcel(); $objPHPExcel5 = new \PHPExcel_Reader_Excel5($objPHPExcel); $objPHPExcel2007 = new \PHPExcel_Reader_Excel2007($objPHPExcel); //查询数据 $data = Db::table("rx_user")->select() ; // var_dump( $data); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1', 'ID编号') ->setCellValue('B1', '用户名') ->setCellValue('C1', '手机号') ->setCellValue('D1', '余额') ->setCellValue('E1', '手续费') ->setCellValue('F1', '身份证号') ->setCellValue('G1', '名字'); $i=2; //定义一个i变量,目的是在循环输出数据是控制行数 $count = count($data); //计算有多少条数据 for ($i = 2; $i <= $count+1; $i++) { $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $data[$i-2]["id"]); $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $data[$i-2]["name"]); $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $data[$i-2]["tel"]); $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $data[$i-2]["balance"]); $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $data[$i-2]["jbalance"]); $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $data[$i-2]["cardno"]); $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $data[$i-2]["realname"]); } $objPHPExcel->getActiveSheet()->setTitle('user'); //设置sheet的名称 $objPHPExcel->setActiveSheetIndex(0); //设置sheet的起始位置 $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); //通过PHPExcel_IOFactory的写函数将上面数据写出来 $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007"); header('Content-Disposition: attachment;filename="用户信息.xlsx"'); header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件 }
<form action="{:url('phpdc')}" enctype="multipart/form-data" method="post">
<input type="submit" style="background:#1890ff;border-color:#1890ff;margin-left:10px;" class="btn btn-sm btn-primary btn-sm btn-danger pull-left" value="导出" />
</from>
https://github.com/PHPOffice/PHPExcel