基于YII框架的PHPExcel导入

静态页面如下:

form onsubmit="return iframeCallback(this, dialogAjaxDone);" target="_self" action="assets/ImportDataFromExcel" method="post" enctype="multipart/form-data">
    <input type="hidden" name="leadExcel" value="true">
    <table align="center" width="90%" border="0">
        <tr>
            <td>
                <input type="file" name="inputExcel"><input type="submit" value="导入数据">
            </td>
        </tr>
    </table>
</form>

<div class="imgtable" style=" margin-top: 30px">
    <a href="/upload/card模板.xls">下载Excel模板</a>
</div>


后台代码导入:

public function actionImportDataFromExcel()
{
   $data = array();
   if($_POST['leadExcel'] == "true") {
       $filename = $_FILES['inputExcel']['name'];
       $tmp_name = $_FILES['inputExcel']['tmp_name'];
       $this->uploadFile($filename, $tmp_name);
   }

   $this->render('importDataFromExcel', array( 'data'=>$data ) );
}

//导入Excel文件
function uploadFile($file,$filetempname)
{
   //自己设置的上传文件存放路径
   $filePath = 'upload/upFile/';
   //下面的路径按照你PHPExcel的路径来修改
   include_once(dirname(dirname(__FILE__)).'/extensions/PHPExcel/PHPExcel.php');
   include_once(dirname(dirname(__FILE__)).'/extensions/PHPExcel/PHPExcel/IOFactory.php');
   include_once(dirname(dirname(__FILE__)).'/extensions/PHPExcel/PHPExcel/Reader/Excel5.php');
   //注意设置时区
   $time=date("y-m-d-H-i-s");//去当前上传的时间
   //获取上传文件的扩展名
   $extend=strrchr ($file,'.');
   //上传后的文件名
   $name=$time.$extend;
   $uploadfile=$filePath.$name;//上传后的文件名地址
   //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
   $result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
   if($result) //如果上传文件成功,就执行导入excel操作
   {
       $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
       $objPHPExcel = $objReader->load($uploadfile);
       $sheet = $objPHPExcel->getSheet(0);
       $objWorksheet = $objPHPExcel->getActiveSheet();
       $highestRow = $objWorksheet->getHighestRow();
       $highestColumn = $objWorksheet->getHighestColumn();
       $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
       $strs=array();
       for ($row = 1;$row <= $highestRow;$row++)
       {
           //注意highestColumnIndex的列数索引从0开始
           for ($col = 0;$col < $highestColumnIndex;$col++)
           {
               $strs[$row-1][$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
           }
       }
   }
   $data =array();
   $data[0] = $strs[0];
   $data[1] = $strs[1];
   unset($strs[0]);
   unset($strs[1]);

   foreach($strs as $k => $v){
       $n = $k+1;
       foreach($v as $kk => $vv){
           //组合Excel中的字段名与值
           $data[$k][$data[1][$kk]] = $vv;
          if($vv == ""){
               $name = $data[0][$kk];
               dwzHelper::jsonError("第.$n.行.$name.不能为空!" ,'assets_cardmng');
           }
       }
   }

   
   //////////////得到最新导入数组,开始插入数据库///////////////////
   if(!is_array($data) || count($data) == 0) {
       dwzHelper::jsonError("导入的数据不合法!" ,'assets_cardmng');
   }
   unset($data[0]); unset($data[1]);
   foreach($data as $kkk=>$vvv){
       $model = new AssetscardTmp();
       $n = $key+1;
       foreach($vvv as $key=>$val){
           $model->$key = $val;
       }
       $model->num = 1;
       $model->id_status = 1;
       $model->cardId = $kkk;
       $model->id_useUnit = Yii::app()->user->did;
       $model->id_useOffice = Yii::app()->user->officeId;
       $model->name_useUnit = Yii::app()->user->name_department;
       $model->name_useOffice = Yii::app()->user->name_office;
       $model->name_responsibility = Yii::app()->user->name;
       $treeInfo = Tree::model()->find('type=:type and id=:id'  , array(':type' => 1,':id'=>$model->id_useUnit));
       $model->series_useUnit = $treeInfo->series;
       $model->cardId =  $this->_assets_model->__getNewCardId();
       $model->createDate = date("y-m-d h:i:s",time());
       $model->setIsNewRecord(true);
       if(!$model->validate() || $model->save() === false){
           dwzHelper::jsonError("第.$n.行导入失败!" ,'assets_cardmng');
       }
   }
   dwzHelper::jsonOk('卡片信息导入成功!' ,'assets_cardmng');
}


你可能感兴趣的:(PHP)