tp5.1 phpspreadsheet- 工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西,)


phpspreadsheet-工具类 导入导出(整合优化,非原创,抄一抄,加了一些自己的东西)
1. composer require phpoffice/phpspreadsheet
2. 看最下面的两个demo  (使用框架 : tp5.1 , laravel也能用,改改不就行了..)

  1 <?php
  2 namespace app\common\utils;
  3 
  4 use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5 use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  6 use PhpOffice\PhpSpreadsheet\IOFactory;
  7 
  8 
  9 /**
 10  * phpoffice-phpspreadsheet-工具类
 11  */
 12 class PhpOffice{
 13 
 14     /**
 15      * @title 导出表格
 16      * @param arr:要导出excel表的数据,接受一个二维数组
 17      * @param fileName:excel表的表名
 18      * @param headAr:excel表的表头,接受一个一维数组
 19      * @param keyAr:$arr中对应表头的键的数组,接受一个一维数组
 20      */
 21     public function exportExcel($fileName='表格', $arr=[], $headAr=[], $keyAr=[]){
 22         $count = count($headAr)-1;//计算所需表头数量
 23 
 24         $spreadsheet = new Spreadsheet();
 25         $sheet = $spreadsheet->getActiveSheet();
 26 
 27         //设置列
 28         $rowAr=self::setRowAr($count);
 29         foreach ($rowAr as $k => $v) {
 30             if($k>$count) break;
 31             $sheet->setCellValue($v.'1',$headAr[$k]);
 32         }
 33  
 34         //写入值
 35         foreach ($arr as $k => $v) {
 36             foreach ($rowAr as $ke => $ve){
 37                 if($ke>$count) break;
 38                 $sheet->setCellValue($ve.($k+2),$v[$keyAr[$ke]]);
 39                 $spreadsheet->getActiveSheet()->getColumnDimension($ve)->setWidth(10); //固定列宽
 40             }   
 41         }
 42         //冻结首行
 43         $spreadsheet->getActiveSheet()->freezePaneByColumnAndRow(0,2);
 44 
 45         //在输出Excel前,缓冲区中处理BOM头
 46         ob_end_clean();
 47         ob_start();
 48 
 49         header('Content-Type: application/vnd.ms-excel');
 50         header('Content-Disposition: attachment;filename="'.$fileName.date('_Ymd_Hi',time()).'.xlsx"');
 51         header('Cache-Control: max-age=0');
 52         $writer = new Xlsx($spreadsheet);
 53         $writer->save('php://output');
 54  
 55         //删除清空:
 56         $spreadsheet->disconnectWorksheets();
 57         unset($spreadsheet);
 58         exit;
 59     }
 60 
 61     //导入
 62     public function importExcel(){
 63         set_time_limit(0);  
 64 
 65         //文件上传导入
 66         // $fileController=new FileController();
 67         $res=self::uploadFileImport();
 68         $data=$res['data'];
 69         //手动输入路径
 70         // $data='muban/muban.xlsx';
 71 
 72         //修正路径
 73         $filename=str_replace('/uploads', 'uploads',$data);
 74         //进行读取
 75         $spreadsheet = IOFactory::load($filename);
 76         $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
 77         return $sheetData;
 78     }
 79 
 80     //文件上传,导入文件专用,数据不入库
 81     public function uploadFileImport(){
 82         // 获取表单上传文件
 83         $file = \request()->file('file');
 84         $return  = array('status' => 1, 'info' => '上传成功', 'data' => []);
 85         // 移动到框架应用根目录/public/uploads/ 目录下
 86         if($file){
 87             $savePath = '/uploads/importExcel';
 88             $info = $file->move(ROOT_PATH . 'public' . DS .$savePath);
 89             $md5 = $info->hash('md5');
 90             $sha1= $info->hash('sha1');
 91             $saveName=strtr($info->getSaveName(),'\\','/');
 92             return self::setResAr(1,'上传成功',$savePath.'/'.$saveName);
 93         }
 94         return self::setResAr(0,'上传失败');
 95     }
 96 
 97     //设置列一维数组(最多701列)
 98     public function setRowAr($count=26){
 99         $indData='A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z';
100         $indData=explode(',',$indData);
101         $curCount=26;
102         for ($i=0; $i <26 ; $i++) { 
103             for ($j=0; $j <26 ; $j++) { 
104                 if($curCount>=$count) return $indData;
105                 $indData[]=$indData[$i].$indData[$j];
106                 $curCount++;
107             }
108         }
109         return $indData;
110     }
111 
112     //设置返回结果格式
113     public function setResAr($code=0,$msg='',$data=array()){
114         return ['code'=>$code,'msg'=>$msg,'data'=>$data];
115     }
116 
117 
118     // 如何实现..
119     // //定义好命名空间
120     // use app\common\utils\PhpOffice;
121     // use think\Db;
122         
123     // class Demo{
124     //     //导出demo-实现
125     //     public function exportDemo(){
126     //         //获取数据
127     //         $data=Db::name('config')->select();
128     //         //获取表头(自行定义)
129     //         $sql="select COLUMN_NAME as name,column_comment as val from INFORMATION_SCHEMA.Columns where table_name='表名'";
130     //         $arr=Db::query($sql);
131 
132     //         $headAr=[];
133     //         $keyAr=[];
134     //         foreach ($arr as $k => $v) {
135     //             $headAr[]=$v['val'];
136     //             $keyAr[]=$v['name'];
137     //         }
138     //         $phpOffice = new PhpOffice();
139     //         $phpOffice->exportExcel('导出表格',$data,$headAr,$keyAr);
140     //     }
141 
142     //     //导入demo-实现
143     //     public function importDemo(){
144     //         $phpOffice = new PhpOffice();
145     //         $data=$phpOffice->importExcel();
146     //         dump($data);
147     //         //自行foreach进行数据处理..
148     //         //..
149     //     }
150     // }
151     // 
152 
153 
154 
155 }

 

 1 <?php
 2 //定义好命名空间
 3 use app\common\utils\PhpOffice;
 4 use think\Db;
 5 
 6 class Demo{
 7 
 8     //导出demo-实现
 9     public function exportDemo(){
10         //获取数据
11         $data=Db::name('config')->select();
12         //获取表头(自行定义)
13         $sql="select COLUMN_NAME as name,column_comment as val from INFORMATION_SCHEMA.Columns where table_name='表名'";
14         $arr=Db::query($sql);
15 
16         $headAr=[];
17         $keyAr=[];
18         foreach ($arr as $k => $v) {
19             $headAr[]=$v['val'];
20             $keyAr[]=$v['name'];
21         }
22         $phpOffice = new PhpOffice();
23         $phpOffice->exportExcel('导出表格',$data,$headAr,$keyAr);
24     }
25 
26     //导入demo-实现
27     public function importDemo(){
28         $phpOffice = new PhpOffice();
29         $data=$phpOffice->importExcel();
30         dump($data);
31         //自行foreach进行数据处理..
32         //..
33     }
34 }
35 ?> 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM