由于项目需求,自行开发定制了一个excel导入功能,网上找的thinkphp例子是不是3.2适用的,所以只能自己改了,本例子适用的thinkphp是Onethink后台插件例子,动手能力强的,可以自行改造,谢谢
1.控制器- <?php
- namespace Admin\Controller;
- /**
- * Excel导入/导出控制器
- * @author crx349 <842062626@qq.com>
- */
- class ExcelController extends AdminController {
- /**
- * 默认显示
- * @author crx349 <842062626@qq.com>
- */
- function index(){
- //$this->display();
- echo "ok";
- }
- /**
- * Excel导入函数
- * @author crx349 <842062626@qq.com>
- */
- function excelto(){
- if (!empty($_FILES)) {
- //import("@.ORG.UploadFile");
- $config = array(
- 'maxSize' => 3145728,
- 'rootPath' => './Uploads/',
- 'savePath' => '/Excel/',
- 'saveName' => array('uniqid',''),
- 'exts' => array('xlsx','xls'),
- 'autoSub' => true,
- 'subName' => array('date','Ymd'),
- );
- $upload = new \Think\Upload($config);// 实例化上传类
- $info = $upload->upload();
- if(!$info) {// 上传错误提示错误信息
- $this->error($upload->getError());
- }else{
- foreach($info as $file) {
- $file_name = $config['rootPath'].$file['savepath'].$file['savename'];
- }
- }
- vendor("PHPExcel.PHPExcel");
- $objReader = \PHPExcel_IOFactory::createReader('Excel5');
- $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
- $sheet = $objPHPExcel->getSheet(0);
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- $highestColumn = $sheet->getHighestColumn(); // 取得总列数
- //第三行B列起
- for($i=3;$i<=$highestRow;$i++)
- {
-
- $data['username']= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();//姓名
- $data['idcard']= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();//身份证
- $data['testdate']= strtotime($objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue());//日期
- $data['course_id']= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();//科目
- $data['number']= '0';//默认号数
- M('personnel')->add($data);
- //var_dump($data);
- //exit;
- }
- $this->success('导入成功!');
- }else
- {
- $this->error("请选择上传的文件");
- }
- }
- }
复制代码
2.视图
- <div class="span{$addons_config.width}">
- <div class="columns-mod">
- <div class="hd cf">
- <h5>排号人员Excel导入</h5>
- <div class="title-opt">
- </div>
- </div>
- <div class="bd">
- <div class="sys-info">
- <table>
- <tr>
- <th>导入</th>
- <td><form action="/admin.php?s=/excel/excelto" method="post" enctype="multipart/form-data">
- <input type="file" name="import"/>
- <input type="hidden" name="table" value="tablename"/>
- <input type="submit" value="导入"/>
- </form></td>
- </tr>
- <tr>
- <th>格式</th>
- <td><a href="/Uploads/Excel/gs.xls">演示xls格式</a> </td>
- </tr>
- </table>
- </div>
- </div>
- </div>
- </div>
复制代码
|