聯系官方銷售客服
1835022288
028-61286886
基于PHPoffice類去做excel表格數據導入導出,可以導出圖片到excel,本人親測一次性導入6000+條簡單數據沒什么問題,很快,在多就沒試過
前端效果如下
導入,要點擊確定才會進行導入數據
導出,如果不選擇數據會提示選擇數據然后進行導出
第一步,下載上傳的附件并將文件解壓到你的項目中,具體目錄如下圖所示。入不放心我上傳的附件,可自行安裝composer進行下載地址如下
https://phpspreadsheet.readthedocs.io/en/stable/
PS:在導入Excel的時候,要使用到文件上傳接口,所以要現在后臺創建一個文件上傳字段,完成之后,要看一下上傳接口對應的參數拿過來,然后替換我對應文件上傳接口中的參數,這個文件上傳字段不要刪除,如果不想要在后臺跟前臺顯示可以選擇隱藏。
這是我使用的文件上傳接口案列
http://census.cc/index.php?s=api&c=file&siteid=1&m=upload&p=020424c201c395dba90596754de3d341&fid=45
如何查看創建字段的上傳接口以及參數
進入到后臺添加頁面,點f12進入到network中然后進行上傳文件,上傳之后,接口會在這里顯示,打開之后,拿到對應的參數,并填入到,我寫的ajax文件上傳接口中對應的參數即可
第二步,在頁面中添加導入導出按鈕,我是加在對應模塊的頭部
代碼如下:
前端js部分代碼:
html部分
<ul class="page-breadcrumb"> {$menu} <li> <link type="text/css" rel="stylesheet" href="{THEME_PATH}from/layui/css/layui.css" /> <div class="layui-upload"> <button type="button" style="background: #fff; color: #888; padding: 0 0" class="layui-btn layui-btn-normal" id="excelImport"><i class="fa fa-plus"></i>導入</button> <i style="color: #DDDDDD" class="fa fa-circle"></i> </div> </li> <li> <a onclick="dr_module_excelExport()" id="excelExport" class=""> <i class="fa fa-minus"></i> 導出</a> <i class="fa fa-circle"></i> </li> </ul>
php部分
<?php namespace Phpcmf\Controllers\Admin; require 'vendor/autoload.php'; //寫入口文件 use PhpOffice\PhpSpreadsheet\Spreadsheet; //引入類,這三個都要引入 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; /** * 二次開發時可以修改本文件,不影響升級覆蓋 */ class Home extends \Phpcmf\Admin\Module { //獲取要導出的數據id并返回給ajax public function dataExport(){ $arr = $_POST['data']; $data = implode(',',$arr); return json_encode($data); } //數據導出 public function excelExport() { $id = $_GET['id']; $line = 0; $spreadsheet = new Spreadsheet(); $image = new Drawing(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValueByColumnAndRow(1, 1, '戶主關系');//這些數據對應excel的列信息 $sheet->setCellValueByColumnAndRow(2, 1, '姓名'); $sheet->setCellValueByColumnAndRow(3, 1, '證件名稱'); $sheet->setCellValueByColumnAndRow(4, 1, '證件號碼'); $sheet->setCellValueByColumnAndRow(5, 1, '籍貫'); $sheet->setCellValueByColumnAndRow(6, 1, '年齡'); $sheet->setCellValueByColumnAndRow(7, 1, '性別'); $sheet->setCellValueByColumnAndRow(8, 1, '戶籍地'); $sheet->setCellValueByColumnAndRow(9, 1, '手機座機'); $sheet->setCellValueByColumnAndRow(10, 1, '座機電話'); $sheet->setCellValueByColumnAndRow(11, 1, '出生時間'); $sheet->setCellValueByColumnAndRow(12, 1, '死亡時間'); $sheet->setCellValueByColumnAndRow(13, 1, '是否低保'); $sheet->setCellValueByColumnAndRow(14, 1, '是否享受生活救助'); $sheet->setCellValueByColumnAndRow(15, 1, '是否暫住'); $sheet->setCellValueByColumnAndRow(16, 1, '暫住地'); $sheet->setCellValueByColumnAndRow(17, 1, '是否流動人口'); $sheet->setCellValueByColumnAndRow(18, 1, '是否矯正人員'); $sheet->setCellValueByColumnAndRow(19, 1, '是否涉毒人員'); $sheet->setCellValueByColumnAndRow(20, 1, '是否殘疾'); $sheet->setCellValueByColumnAndRow(21, 1, '用人單位'); $sheet->setCellValueByColumnAndRow(22, 1, '社保信息'); $sheet->setCellValueByColumnAndRow(23, 1, '個人健康狀況'); $sheet->setCellValueByColumnAndRow(24, 1, '實際居住地'); $sheet->setCellValueByColumnAndRow(25, 1, '實際居住詳細地址'); $sheet->setCellValueByColumnAndRow(26, 1, '照片'); $styleArray = [ 'alignment' => [ 'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, ], ]; //查詢并獲取要導出的數據 $data = \Phpcmf\Service::M()->db->table(SITE_ID.'_census') ->select(' relation,name,head,card_name,card_id,hometown,age,sex,domicile,phone, tel,birthday,deadtime,is_guarantee,is_assistance,is_residence,temporary, population,correction,is_poison,is_deformity,employer,shebaoxinxi,health, residence,address ') ->where('id in('.$id.')') ->get()->getResultArray(); $relation = dr_field_options(16); $card_name = dr_field_options(19); $sex = dr_field_options(23); $is_guarantee = dr_field_options(30); $is_assistance = dr_field_options(31); $is_residence = dr_field_options(32); $population = dr_field_options(34); $correction = dr_field_options(35); $is_poison = dr_field_options(36); $is_deformity = dr_field_options(37); $shebaoxinxi = dr_field_options(39); $health = dr_field_options(40); //通過循環去匹配數據 for ($i = 0; $i < count($data); $i++) { $line = $i + 2;//代表從第幾行開始 //$sheet->setCellValueByColumnAndRow(1)中的1,2,3代表上面的那一列 $sheet->setCellValueByColumnAndRow(1, $line, $relation[$data[$i]['relation']]); $sheet->setCellValueByColumnAndRow(2, $line, $data[$i]['name']); $sheet->setCellValueByColumnAndRow(3, $line, $card_name[$data[$i]['card_name']]); $sheet->setCellValueByColumnAndRow(4, $line, $data[$i]['card_id']); $sheet->setCellValueByColumnAndRow(5, $line, $data[$i]['hometown']); $sheet->setCellValueByColumnAndRow(6, $line, $data[$i]['age']); $sheet->setCellValueByColumnAndRow(7, $line, $sex[$data[$i]['sex']]); $sheet->setCellValueByColumnAndRow(8, $line, $data[$i]['domicile']); $sheet->setCellValueByColumnAndRow(9, $line, $data[$i]['phone']); $sheet->setCellValueByColumnAndRow(10, $line, $data[$i]['tel']); $sheet->setCellValueByColumnAndRow(11, $line, dr_date($data[$i]['birthday'],'Y-m-m H:i:s')); $sheet->setCellValueByColumnAndRow(12, $line, dr_date($data[$i]['deadtime'],'Y-m-m H:i:s')); $sheet->setCellValueByColumnAndRow(13, $line, $is_guarantee[$data[$i]['is_guarantee']]); $sheet->setCellValueByColumnAndRow(14, $line, $is_assistance[$data[$i]['is_assistance']]); $sheet->setCellValueByColumnAndRow(15, $line, $is_residence[$data[$i]['is_residence']]); $sheet->setCellValueByColumnAndRow(16, $line, $data[$i]['temporary']); $sheet->setCellValueByColumnAndRow(17, $line, $population[$data[$i]['population']]); $sheet->setCellValueByColumnAndRow(18, $line, $correction[$data[$i]['correction']]); $sheet->setCellValueByColumnAndRow(19, $line, $is_poison[$data[$i]['is_poison']]); $sheet->setCellValueByColumnAndRow(20, $line, $is_deformity[$data[$i]['is_deformity']]); $sheet->setCellValueByColumnAndRow(21, $line, $data[$i]['employer']); $sheet->setCellValueByColumnAndRow(22, $line, $shebaoxinxi[$data[$i]['shebaoxinxi']]); $sheet->setCellValueByColumnAndRow(23, $line, $health[$data[$i]['health']]); $sheet->setCellValueByColumnAndRow(24, $line, dr_linkagepos('address', $data[$i]['residence'], ' - ')); $sheet->setCellValueByColumnAndRow(25, $line, $data[$i]['address']); $url = parse_url(dr_thumb($data[$i]['head']));//獲取圖片對應的地址 //使用圖片導出類 $drawing[$line] = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing(); $drawing[$line]->setName('頭像'); $drawing[$line]->setDescription('頭像'); $drawing[$line]->setPath('.'.$url['path']); $drawing[$line]->setWidth(100); $drawing[$line]->setHeight(100); $drawing[$line]->setCoordinates('Z'.$line);//z代表某一咧 $drawing[$line]->setOffsetX(0); $drawing[$line]->setOffsetY(0); $drawing[$line]->setWorksheet($spreadsheet->getActiveSheet()); $spreadsheet->getActiveSheet()->getRowDimension($line)->setRowHeight(100);//設置高度 } $filename = '戶籍信息管理表('.date('YmdHis',SYS_TIME).').xlsx';//導出表格名稱 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0'); //設置樣式 $spreadsheet->getActiveSheet()->getColumnDimension('A')->setWidth(12); $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(12); $spreadsheet->getActiveSheet()->getColumnDimension('C')->setWidth(15); $spreadsheet->getActiveSheet()->getColumnDimension('D')->setWidth(25); $spreadsheet->getActiveSheet()->getColumnDimension('E')->setWidth(15); $spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(5); $spreadsheet->getActiveSheet()->getColumnDimension('G')->setWidth(5); $spreadsheet->getActiveSheet()->getColumnDimension('H')->setWidth(15); $spreadsheet->getActiveSheet()->getColumnDimension('I')->setWidth(15); $spreadsheet->getActiveSheet()->getColumnDimension('J')->setWidth(15); $spreadsheet->getActiveSheet()->getColumnDimension('K')->setWidth(18); $spreadsheet->getActiveSheet()->getColumnDimension('L')->setWidth(18); $spreadsheet->getActiveSheet()->getColumnDimension('M')->setWidth(9); $spreadsheet->getActiveSheet()->getColumnDimension('N')->setWidth(17); $spreadsheet->getActiveSheet()->getColumnDimension('O')->setWidth(9); $spreadsheet->getActiveSheet()->getColumnDimension('P')->setWidth(35); $spreadsheet->getActiveSheet()->getColumnDimension('Q')->setWidth(13); $spreadsheet->getActiveSheet()->getColumnDimension('R')->setWidth(13); $spreadsheet->getActiveSheet()->getColumnDimension('S')->setWidth(13); $spreadsheet->getActiveSheet()->getColumnDimension('T')->setWidth(9); $spreadsheet->getActiveSheet()->getColumnDimension('U')->setWidth(35); $spreadsheet->getActiveSheet()->getColumnDimension('V')->setWidth(85); $spreadsheet->getActiveSheet()->getColumnDimension('W')->setWidth(13); $spreadsheet->getActiveSheet()->getColumnDimension('X')->setWidth(35); $spreadsheet->getActiveSheet()->getColumnDimension('Y')->setWidth(35); $spreadsheet->getActiveSheet()->getColumnDimension('Z')->setWidth(26); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); } //表格導入 public function excelImport() { $excel = parse_url($_POST['url']); $type = ucfirst($_POST['type']); $fileid = $_POST['id']; $this->db = \Config\Database::connect('default'); $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($type); $reader->setReadDataOnly(TRUE); $spreadsheet = $reader->load('.'.$excel['path']); //載入excel表格 $worksheet = $spreadsheet->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); // 總行數 $highestColumn = $worksheet->getHighestColumn(); // 總列數 $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); // e.g. 5 $lines = $highestRow - 1; if ($lines <= 0) { exit('Excel表格中沒有數據'); } $sql = "INSERT INTO 'dr_".SITE_ID."_census' ('relation','name','head','card_name','card_id','hometown','age','sex','domicile','phone', 'tel','birthday','deadtime','is_guarantee','is_assistance','is_residence','temporary', 'population','correction','is_poison','is_deformity','employer','shebaoxinxi','health', 'residence','address') VALUES "; $data = []; $relation = dr_field_options(16); $card_name = dr_field_options(19); $sex = dr_field_options(23); $is_guarantee = dr_field_options(30); $is_assistance = dr_field_options(31); $is_residence = dr_field_options(32); $population = dr_field_options(34); $correction = dr_field_options(35); $is_poison = dr_field_options(36); $is_deformity = dr_field_options(37); $shebaoxinxi = dr_field_options(39); $health = dr_field_options(40); //獲取表格中的數據 for ($row = 0; $row < $lines; $row++) { $data[$row]['url'] = $worksheet->getCellByColumnAndRow(1, $row+2)->getValue(); foreach ($relation as $key => $re) { if ($re === $worksheet->getCellByColumnAndRow(2, $row+2)->getValue()) { $data[$row]['relation'] = $key; } } $data[$row]['name'] = $worksheet->getCellByColumnAndRow(3, $row+2)->getValue(); foreach ($card_name as $key => $ca) { if ($ca === $worksheet->getCellByColumnAndRow(4, $row+2)->getValue()) { $data[$row]['card_name'] = $key; } } $data[$row]['card_id'] = $worksheet->getCellByColumnAndRow(5, $row+2)->getValue(); $data[$row]['hometown'] = $worksheet->getCellByColumnAndRow(6, $row+2)->getValue(); $data[$row]['age'] = $worksheet->getCellByColumnAndRow(7, $row+2)->getValue(); foreach ($sex as $key => $se) { if ($se === $worksheet->getCellByColumnAndRow(8, $row+2)->getValue()) { $data[$row]['sex'] = $key; } } $data[$row]['domicile'] = $worksheet->getCellByColumnAndRow(9, $row+2)->getValue(); $data[$row]['phone'] = $worksheet->getCellByColumnAndRow(10, $row+2)->getValue(); $data[$row]['tel'] = $worksheet->getCellByColumnAndRow(11, $row+2)->getValue(); $data[$row]['birthday'] = strtotime($worksheet->getCellByColumnAndRow(12, $row+2)->getValue()); $data[$row]['deadtime'] = strtotime($worksheet->getCellByColumnAndRow(13, $row+2)->getValue()); foreach ($is_guarantee as $key => $is_gu) { if ($is_gu === $worksheet->getCellByColumnAndRow(14, $row+2)->getValue()) { $data[$row]['is_guarantee'] = $key; } } foreach ($is_assistance as $key => $is_as) { if ($is_as === $worksheet->getCellByColumnAndRow(15, $row+2)->getValue()) { $data[$row]['is_assistance'] = $key; } } foreach ($is_residence as $key => $is_re) { if ($is_re === $worksheet->getCellByColumnAndRow(16, $row+2)->getValue()) { $data[$row]['is_residence'] = $key; } } $data[$row]['temporary'] = $worksheet->getCellByColumnAndRow(17, $row+2)->getValue(); foreach ($population as $key => $po) { if ($po === $worksheet->getCellByColumnAndRow(18, $row+2)->getValue()) { $data[$row]['population'] = $key; } } foreach ($correction as $key => $co) { if ($co === $worksheet->getCellByColumnAndRow(19, $row+2)->getValue()) { $data[$row]['correction'] = $key; } } foreach ($is_poison as $key => $is_po) { if ($is_po === $worksheet->getCellByColumnAndRow(20, $row+2)->getValue()) { $data[$row]['is_poison'] = $key; } } foreach ($is_deformity as $key => $is_de) { if ($is_de === $worksheet->getCellByColumnAndRow(21, $row+2)->getValue()) { $data[$row]['is_deformity'] = $key; } } $data[$row]['employer'] = $worksheet->getCellByColumnAndRow(22, $row+2)->getValue(); foreach ($shebaoxinxi as $key => $she) { if ($she === $worksheet->getCellByColumnAndRow(23, $row+2)->getValue()) { $data[$row]['shebaoxinxi'] = $key; } } foreach ($health as $key => $he) { if ($he === $worksheet->getCellByColumnAndRow(24, $row+2)->getValue()) { $data[$row]['health'] = $key; } } $address = explode(' - ',$worksheet->getCellByColumnAndRow(25, $row+2)->getValue()); foreach ($address as $add) {} $area = \Phpcmf\Service::M()->db->table('linkage_data_1')->where('name', $add)->get()->getResultArray(); $data[$row]['residence'] = $area[0]['id']; $data[$row]['address'] = $worksheet->getCellByColumnAndRow(26, $row+2)->getValue(); $data[$row]['head'] = $worksheet->getCellByColumnAndRow(27, $row+2)->getValue(); $data[$row]['catid'] = 1; $data[$row]['inputip'] = \Phpcmf\Service::L('input')->ip_address(); $data[$row]['inputtime'] = SYS_TIME; $data[$row]['updatetime'] = SYS_TIME; } //將從Excel表格中獲取到的數據插入到數據庫中 foreach ($data as $key => $da){ if ($da['url'] == null || $da['url'] == ' '){ $da['fid'] = 0; $info = $this->db->table(SITE_ID.'_census')->insert($da); if ($info){ $fid = $this->db->insertID(); $card_id = $da['card_id']; if ($info) { $code = 1; }else{ $code = 0; $total = $key+2; } } }else{ if ($da['url'] == $card_id){ $da['fid'] = $fid; $da['url'] = ' '; $info = $this->db->table(SITE_ID.'_census')->insert($da); if ($info) { $code = 1; }else{ $code = 0; $total = $key+2; } } } } if ($info) { \Phpcmf\Service::M('Attachment')->file_delete($this->member['id'],$fileid);//刪除附件,如果導入成功之后不想刪除附件,刪除這句話即可 return json_encode(['code'=>$code]); }else { \Phpcmf\Service::M('Attachment')->file_delete($this->member['id'],$fileid); return json_encode(['code'=>$code],['total'=>$total]); } } }
如需定制,可以加我QQ1191892256或者私聊我,隨時有空,有什么問題也可以問我。順便推廣一下我的火車頭采集插件,任何模塊都可以導入,根據獨立跟共享模塊接口,分別可以獲取所有模塊的欄目,多模塊,多文件,隨機會員。需要的可以看看
參考文檔:《火車頭內容采集》
火車頭采集器
火車頭采集器,即裝即用,支持所有模塊,多文件采集,默認news模塊,可以發布獨立模塊以及...
哈,這個牛逼,看下
開箱即用,粘貼復制就可以了,改一下里面要導出的字段就ok,圖片也可以導出,看不懂可以問我,不過最好看一下手冊,都有,用的時候,把html。js這些原樣復制過去就可以了,后臺也是,后臺代碼就需要改要導出的字段,其他的不要改
回復@叢林灰太狼
回復@琉惗 php的 把導出字段修改為我自己的字段就可以了是嗎?這個可以用于pos3不,哥們!
可以的,方法一樣的,不管pos3跟訊睿都可以,前端不要改,就要改后端的那個數據查詢方式,數據查詢方式不一樣,其他的一模一樣
比如這樣的方式,pos3就不一樣的話就要改這樣的方式就可以了
回復@叢林灰太狼
哈哈 多謝之,兄弟,我現在先試試,不懂在群里還是這么問?
回復@琉惗
不懂可以加我QQ或者私聊我,我會給你說的1191892256
回復@叢林灰太狼
哈哈 我就是群里的UU,我有你QQ呢,兄弟!
回復@琉惗
好吧.
回復@叢林灰太狼
厲害,學習學習
厲害,學習學習
這兩個要刪掉,是系統自己加上的,js部分改成這樣
js的改成這樣
js改成這樣.....系統會自己多加個東西......
哈哈,樓主水平相當牛,關鍵是相當的熱心,搞不懂的直接遠程幫解決,非常感謝,建議可以獨立,平時在群里也非常熱心,建議有開發需求的朋友,需要外包的可以找他,哈哈!
建議可以獨立成插件,少打幾個字了 哈哈
回復@叢林灰太狼
厲害,學習學習
做成插件 ,很多人需要
哦,我覺得這個東西已經是可以用了,就看需要的人懶不懶愿不愿意改一下里面的要導出的字段就行了,你可以問問樓上的那個叢林森林狼。他已經會了,很簡單,所以我覺得人人都可以自己做,就沒做插件,不過馬上就要做了,有客戶需要.........
回復@湘西北的風
不錯 看看
本部分內容設定了隱藏,需要回復后才能看到,立即回復
學習一下看能不能用
太強大了
看看
本部分內容設定了隱藏,需要回復后才能看到,立即回復
666666
學習
學習
謝謝分享
看看前端JS
優秀,支持下開發者!
這個插件在哪呢
回復@Supr
還沒上架呢,就是基于模塊的導入導出給你大概看看效果
回復@琉惗 你這個只是一張對應一個模塊。如果有幾十張不是要建立幾十個模塊???
回復@Supr這個是一個模塊對應好多表,就跟你后臺添加數據一樣,當然,單表導入也支持,如果你會單表導入就可以用單表,不會就可以用模塊導入去做,就跟你發布文章是一樣,很簡單,多選,下拉,以及多聯動,復選框,這些都會自己判斷,你只需要選擇要導入的欄目跟導入的字段就可以
回復@琉惗 一個模塊 多張不同表 每張字段不同 這個可以?
回復@Supr可以啊
學習學習
學習下
學習下
學習下
學習下
學習了
回復@jinghuaai3 請問這個怎么安裝,我卡在了在后臺創建導入按鈕了
你的導入導出插件不錯
回復@jinghuaai3 放在模版里面
學一學
學一學
能不能導出表單數據
學習
學習一下
剛接觸 收藏 備用
這個是導出模塊數據的,可以單獨導出表單數據么?
回復@叢林灰太狼 請問這個 census 文件夾是什么?
學習了
這個牛皮了
回復@琉惗
請問
這個文件該怎么寫。。
回復@菜鳥一枚 引用layui文件,去layui官網下載一個然后放進去引用就行
厲害,學習學習
學習學習,感謝分享?。。?/p>
學習下
牛逼,感謝樓主分享。。。。。。。。。