殊珠子 php Excel文件导入 Spreadsheet_Excel_Reader
来源:互联网 发布:唯一网络王宇杰简介 编辑:程序博客网 时间:2024/05/01 01:11
<?phpclass Excel2Db{ public $filePath = "../../../../excel_file/"; public function __construct($filePath) { $this->filePath = $filePath; $this->insertExcel(); } private function insertExcel(){ $file_name = $_FILES['excel']['name']; $tmp_name = $_FILES['excel']['tmp_name']; $file_size = $_FILES['excel']['size']; $file_error = $_FILES['excel']['error']; // 处理excel文件 $this->checkExcel($file_name,$file_size,$file_error); // 上传excel文件 $uploadfilename = $this->d_file_move($file_name,$tmp_name); if (isset($uploadfilename) && !empty($uploadfilename)) { if ( file_exists($uploadfilename) && is_readable($uploadfilename)) { // 读取上传的excel文件并导入数据库 $res_insert= $this->insertExceltoDatabase($uploadfilename); unlink($uploadfilename); //删除之前上传的文件 echo $res_insert; }else{ die("<script>alert('上传文件不可读,请上传可读文件');history.back(-1);</script>"); } } else { showMessage("文件上传失败!"); } } /** * 处理excel文件 */ private function checkExcel($file_name,$file_size,$file_error){ if ($file_name != '') { $fileExcel = substr(strrchr($file_name,'.'), 1); if ($fileExcel != 'xls') { die("<script>alert('文件格式不正确,只可上传.xls类型的文件');history.back(-1);</script>"); } if($file_size>512000){ die("<script>alert('文件过大,最大为500K');history.back(-1);</script>"); } if($file_error!=0){ die("<script>alert('未知错误,文件上传失败');history.back(-1);</script>"); } }else{ showMessage("Excel文件上传错误,请检查后重新上传!"); } } /** * 上传文件方法 */ private function d_file_move($file,$filetempname){ $filePath = $this->filePath;//上传文件的路径 if(!is_dir($filePath)) mkdir($filePath);//检查并创建文件夹 $time = date('YmdHis',time());//设置文件名,注意设置时区 $extend = strchr($file,'.');//获取文件后缀 $newname = $time.$extend;//设置上传后完整的文件名 $uploadfile = $filePath.$newname;//文件路径 if(file_exists($uploadfile)){ die("<script>alert('文件已存在,请重新上传');history.back(-1);</script>"); } $res = move_uploaded_file($filetempname,$uploadfile);//通过move_uploaded_file函数移动文件到指定目录 if($res) return $uploadfile;//如果成功返回当前文件路径 return false; } /** * Excel导入数据库 */ private function insertExceltoDatabase($uploadfilename){ $coupon_member_model = Model('coupon_member'); $data=new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('utf-8'); $data->read($uploadfilename); // 注意上传的Excel要是能够正常读取编辑的才可以 $rownum=$data->sheets[0]['numRows']; $str=""; $success=0; $insert_coupon_data = array(); for ($i=2; $i <=$rownum ; $i++) { $coupon_code=trim($data->sheets[0]['cells'][$i][1]); $coupon_pwd=trim($data->sheets[0]['cells'][$i][2]); //验证填写信息是否正确 if (!isset($coupon_code) || empty($coupon_code)) { $str.="抵用券编码为空,第".$i."行\\n"; continue; } if (!isset($coupon_pwd) || empty($coupon_pwd)) { $str.="抵用券密码为空,第".$i."行\\n"; continue; } $coupon_data['coupon_code'] = $coupon_code; $coupon_data['coupon_pwd'] = $coupon_pwd; $insert_coupon_data[] = $coupon_data; $success++; } // 批量插入数据表 if ($success>0) { $res_insert = $coupon_member_model->addCouponMemberAll($insert_coupon_data); return "<script>alert('成功导入".$success."条记录,失败记录:\\n".$str."');history.back(-1);;</script>"; }else{ return false; } }}新手小白一个,希望大神指点!
0 0
- 殊珠子 php Excel文件导入 Spreadsheet_Excel_Reader
- PHP读取Excel 之 Spreadsheet_Excel_Reader和Writer
- php导入excel文件
- PHP导入Excel文件
- php导入excel文件
- php 导入excel文件
- php 导入到excel文件
- PHP导出文件,导入Excel
- php导出导入excel 文件
- Spreadsheet_Excel_Reader导入EXCEL内容到数据库的时候出现的错误
- php向数据库导入excel文件
- php导出和导入Excel文件
- php导入excel文件功能开发 phpExcelReader
- php导入及导出excel文件
- php导入导出excel文件 简介
- php导入excel文件到数据库
- 使用PHP导入Excel和导出数据为Excel文件
- 使用PHP导入Excel和导出数据为Excel文件
- OC 学习回顾一
- 积累一些常用的功能的代码片段。持续更新
- 正版Minecraft的安装
- 电子老鼠闯迷宫
- 1.opencv中的绝对路径问题
- 殊珠子 php Excel文件导入 Spreadsheet_Excel_Reader
- iOS程序设计心得总结(三)应用层设计
- swift实现提示框第三方库:MBProgressHUD
- 希尔排序
- Three.js 添加阴影
- (一)Liferay-可配置portlet
- python学习记录--元组
- 洛谷 P1056 排座椅
- Python学习(三)——map和reduce函数