ThinkPHP3.2.3 PHPExcel讀取excel插入數據庫

版本 ThinkPHP3.2.3 php

下載PHPExcel數據庫

將這兩個文件放到並更更名字this

excel文件:spa

數據庫表:excel

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

代碼:對象

代碼主要在index方法中,有數據提交則寫入數據庫,不然展現表單blog

 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 }
相關文章
相關標籤/搜索