tp 引入phpexcel 進行單表格的導入,在線瀏覽

  百度下載phpexcel插件,而後放進thinkphp裏面。本人是放在ThinkPHP\Library\Vendor裏面的,導出的下的是別人封裝好的類這裏就不展現了。php

如下是單表導入thinkphp

 1 ini_set('memory_limit','1024M');
 2 if (!empty($_FILES)) {
 3   $config = array(
 4   'exts' => array('xlsx','xls'),
 5   'maxSize' => 3145728000,
 6   'rootPath' =>"./Public/",
 7   'savePath' => 'Uploads/',
 8   'subName' => array('date','Ymd'),
 9 );
10 $upload = new \Think\Upload($config);
11 if (!$info = $upload->upload()) {
12   $this->error($upload->getError());}
13   vendor("PHPExcel.PHPExcel"); //這個是vendor的引入方法
14   $file_name=$upload->rootPath.$info['photo']['savepath'].$info['photo']['savename'];
15   $extension = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));//判斷導入表格後綴格式
16   if ($extension == 'xlsx') {
17     $objReader =\PHPExcel_IOFactory::createReader('Excel2007');//注意調用裏面的方法須要加\
18     $objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
19   } else if ($extension == 'xls'){
20     $objReader =\PHPExcel_IOFactory::createReader('Excel5');
21     $objPHPExcel =$objReader->load($file_name, $encode = 'utf-8');
22   }
23   $num1=$objPHPExcel->getSheetCount();
24 
25   $sheet =$objPHPExcel->getSheet(0);
26 
27   $highestRow = $sheet->getHighestRow();//取得總行數
28   $highestColumn =$sheet->getHighestColumn(); //取得總列數
29   $num=0;
30   for ($i = 1; $i <= $highestRow; $i++) {
31 
32     //看這裏看這裏,前面小寫的a是表中的字段名,後面的大寫A是excel中位置
33     $data['a'] =$objPHPExcel->getActiveSheet()->getCell("A" . $i)->getCalculatedValue();
34 
35     //看這裏看這裏,這個位置寫數據庫中的表名
36 
37     M('inboeal')->add($data);
38   }
39   $this->success('導入成功!');
40 } else {
41   $this->error("請選擇上傳的文件");
42 }

 

 

 

如下是在線瀏覽數據庫

 1 $name=$_GET['condition'];
 2 $namelist=substr($name,strlen($name)-4,strlen($name));
 3 
 4 vendor("PHPExcel.PHPExcel");
 5 vendor("PHPExcel.PHPExcel.Writer.Excel5");
 6 vendor("PHPExcel.PHPExcel.Reader.Excel5");
 7 vendor("PHPExcel.PHPExcel.Reader.Excel2007");
 8 vendor("PHPExcel.PHPExcel.Reader.Excel2003XML");
 9 vendor("PHPExcel.PHPExcel.Writer.Excel2007");
10 vendor("PHPExcel.PHPExcel.Writer.HTML");
11 if($namelist=='xlsx'){
12   $objReader = new \PHPExcel_Reader_Excel2007();
13 }else{
14   $objReader = new \PHPExcel_Reader_Excel5();
15 }
16 $objExcel = new \PHPExcel();
17 $objWriteHTML = new \PHPExcel_Writer_HTML($objReader->load('./Public/Uploads/baobiao/'.$name)); //輸出網頁格式的對象。注:writer是讀寫
18 $objWriteHTML->save("php://output");
相關文章
相關標籤/搜索