分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

Thinkphp 使用PHPExcel导入,栗子

发布时间:2023-09-06 01:37责任编辑:白小东关键词:PHP

栗子:

<?php ???public function importData(){ ???????if ( !empty( $_FILES[‘file‘][‘tmp_name‘] ) ) { ???????????$uploadfile = $_FILES [‘file‘] [‘tmp_name‘]; ???} else { ???????????$uploadfile = $_FILES[‘uploadfile‘][‘tmp_name‘]; ???????} ???????????if(empty($uploadfile) or !file_exists($uploadfile)){ ???????????die(‘file not exists‘); ???????} ???????//Include path ???????set_include_path ( get_include_path () . PATH_SEPARATOR . LIB_ROOT_PATH . ‘3rdParty/PHPExcel/‘ ); ???????// PHPExcel_IOFactory ???????include ‘PHPExcel.php‘; ???????include ‘PHPExcel/IOFactory.php‘; ???????????if (!class_exists(‘PHPExcel_IOFactory‘)) { ???????????//PHPExcel类未找到 ???????????echo ‘未知错误!‘; ???????} ???????????????$inputFileType = \PHPExcel_IOFactory::identify( $uploadfile ); ???????????????if ( in_array( $inputFileType, array (‘Excel2007‘, ‘Excel5‘)) ){ ???????????????$objReader = \PHPExcel_IOFactory::createReader( $inputFileType ); ???????????????$objPHPExcel = $objReader->load( $uploadfile ); ???????????????$sheetData = $objPHPExcel->getActiveSheet()->toArray( null, true, true, true ); ???????????????$addData = array(); ???????????????foreach ( $sheetData as $key => $value ) { ???????????????????$addData[‘field1‘] .= $value[‘A‘].‘,‘; ???????????????????$addData[‘field2‘] .= $value[‘B‘].‘,‘; ???????????????????} ???????????????????rtrim($addData[‘field1‘],‘,‘); ???????????????????$addData[‘field3‘] = ‘测试参数‘; ???????????????$addData[‘type‘] = 666; ???????????????????$res = \AppTools::webService(‘\Model\Modelname\Modelname‘, ‘getImport‘, array(‘params‘=>$addData )); ???????????????????if( $res[‘status‘] == 0 && !empty($res[‘data‘][‘fail‘]) ){ ???????????????????//存在导入失败的 ???????????????????$res[‘data‘][‘fail‘] = implode($res[‘data‘][‘fail‘],‘,‘); ???????????????????$res[‘ 1. msg‘] = sprintf($this->translator->failedlist, mb_substr($res[‘data‘][‘fail‘],0,27,‘utf-8‘)); ???????????????????$res[‘status‘] = 1; ???????????????}elseif( $res[‘status‘] == 0 && empty($res[‘data‘][‘fail‘]) ){ ???????????????????//全部导入成功 ???????????????????$res[‘msg‘] = $this->translator->success; ???????????????}else{ ???????????????????//导入出错 ???????????????????$res[‘msg‘] = $this->translator->failed; ???????????????????$res[‘status‘] = ‘error‘; ???????????????} ???????????????????session(‘arraylist‘,$res); ???????????????????$this->redirect(‘index‘); ???????} ???} ????????????>

Thinkphp 使用PHPExcel导入,栗子

原文地址:https://www.cnblogs.com/richardcastle/p/8297179.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved