ThinkPHP3.2.3 PHPExcel读取excel插入数据库

时间:2022-05-20 20:15:48

版本 ThinkPHP3.2.3 

下载PHPExcel

ThinkPHP3.2.3 PHPExcel读取excel插入数据库

将这两个文件放到并更改名字

ThinkPHP3.2.3 PHPExcel读取excel插入数据库

excel文件:

ThinkPHP3.2.3 PHPExcel读取excel插入数据库

数据库表:

CREATE TABLE `sh_name` (
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ThinkPHP3.2.3 PHPExcel读取excel插入数据库

代码:

代码主要在index方法中,有数据提交则写入数据库,否则展示表单

 1 <?php
 2 namespace Home\Controller;
 3 use Think\Controller;
 4 use Think\Upload;
 5 
 6 class IndexController extends Controller {
 7     public function indexAction(){
 8         if(IS_POST){
 9             $file = $_FILES;
10             //上传
11             $path = $this->upload($file);
12             //读取excel
13             $arr = $this->excel($path, 0);
14             //实例化模型
15             $model = D('name');
16             //添加的数据
17             $data = [];
18             for($i=2; $i<=count($arr); $i++){
19                 $data[] = ['name'=>$arr[$i]['A'], 'age'=>$arr[$i]['B']];
20             }
21             //添加
22             $model->addAll($data);
23         }else{
24             $this->display();
25         }
26         
27     }
28     public function testAction(){
29         echo "<h1>hello world</h1>";
30     }
31     //上传
32     public function upload(){
33         $upload = new Upload();
34         $upload->maxSize = 3145728 ;// 设置附件上传大小
35         $upload->exts = array('jpg', 'gif', 'png', 'jpeg', 'xls');// 设置附件上传类型
36         $upload->rootPath = APP_PATH . 'Uploads/'; // 设置附件上传根目录
37         $upload->savePath = 'xls/'; // 设置附件上传(子)目录
38         // 上传文件
39         $info = $upload->upload();
40         $info = $info['inputfile'];
41         if(!$info) {// 上传错误提示错误信息
42             $this->error($upload->getError());
43         }
44 
45         return APP_PATH . 'Uploads/' . $info['savepath'] . $info['savename'];
46     }
47 
48     //excel
49     public function excel($filePath='', $sheet=0){
50         
51         import("Org.Util.PHPExcel");
52         import("Org.Util.PHPExcel.Reader.Excel5");
53         import("Org.Util.PHPExcel.Reader.Excel2007");
54 
55         if(empty($filePath) or !file_exists($filePath)){die('file not exists');}
56         $PHPReader = new \PHPExcel_Reader_Excel2007();        //建立reader对象
57         if(!$PHPReader->canRead($filePath)){
58             $PHPReader = new \PHPExcel_Reader_Excel5();
59             if(!$PHPReader->canRead($filePath)){
60                  echo 'no Excel';
61                 return ;
62             }
63         }
64         $PHPExcel = $PHPReader->load($filePath);        //建立excel对象
65         $currentSheet = $PHPExcel->getSheet($sheet);        //**读取excel文件中的指定工作表*/
66         $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/
67         $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/
68         $data = array();
69         for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){        //循环读取每个单元格的内容。注意行从1开始,列从A开始
70             for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
71                 $addr = $colIndex.$rowIndex;
72                 $cell = $currentSheet->getCell($addr)->getValue();
73                 if($cell instanceof PHPExcel_RichText){ //富文本转换字符串
74                     $cell = $cell->__toString();
75                 }
76                 $data[$rowIndex][$colIndex] = $cell;
77             }
78         }
79         return $data;
80     }  
81 }