import.php

来自「通达OA官方提供的30源代码,感觉很实在」· PHP 代码 · 共 169 行

PHP
169
字号
<?
include_once("inc/auth.php");
include_once("inc/utility_all.php");
?>
<html>
<head>
<title>导入数据</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body class="bodycolor" topmargin="5">
<?
if($FILE_NAME=="")
{

 $query = "SELECT count(*) from SAL_DATA where FLOW_ID='$FLOW_ID'";
 $cursor= exequery($connection,$query);
 if($ROW=mysql_fetch_array($cursor))
 {
   $ROW_COUNT=$ROW[0];
 }
?>
<script Language="JavaScript">
function CheckForm()
{
   if(document.form1.CSV_FILE.value=="")
   { alert("请选择要导入的文件!");
     return (false);
   }

   if (document.form1.CSV_FILE.value!="")
   {
     var file_temp=document.form1.CSV_FILE.value,file_name;
     var Pos;
     Pos=file_temp.lastIndexOf("\\");
     file_name=file_temp.substring(Pos+1,file_temp.length);
     document.form1.FILE_NAME.value=file_name;
   }

   return (true);
}
</script>

  <table border="0" width="100%" cellspacing="0" cellpadding="3" class="small">
    <tr>
<?
   if ($ROW_COUNT!=0)
    {
?>
      <td class="Big"><img src="/images/sys_config.gif" align="absmiddle"><span class="big3"> 导入CSV工资数据(该流程已录入过数据)</span><br>
      </td>
<?
    }
    else
    {
?>
      <td class="Big"><img src="/images/sys_config.gif" align="absmiddle"><span class="big3"> 导入CSV工资数据</span><br>
      </td>
<?
    }
?>
    </tr>
  </table>

  <br>
  <br>

  <div align="center" class="Big1">
  <b>请指定用于导入的CSV文件:</b>
  <form name="form1" method="post" action="import.php?FLOW_ID=<?=$FLOW_ID?>" enctype="multipart/form-data" onSubmit="return CheckForm();">
    <input type="file" name="CSV_FILE" class="BigInput" size="30">
    <input type="hidden" name="FILE_NAME">
    <input type="hidden" name="GROUP_ID" value="<?=$GROUP_ID?>">
    <input type="submit" value="导入" class="BigButton">
  </form>

  <br>
  <input type="button" value="返回" class="BigButton" onClick="location='index.php'">
  <?
  if($FILE_NAME=="")
  Message("说明","1)EXECL的工资报表的列顺序为姓名、工资项目,将部门、职务等列删除;<BR>2)将改好的EXECL工资报表另存为CSV格式的文件;");
  ?>

  </div>
<?
   exit;
}

if(strtolower(substr($FILE_NAME,-3))!="csv")
{
   Message("错误","只能导入CSV文件!");
   Button_Back();
   exit;
}

$query = "SELECT ITEM_ID ,ITEM_NAME from SAL_ITEM";
$cursor= exequery($connection,$query);
$title=array();
while($ROW=mysql_fetch_array($cursor))
{
   $ITEM_NAME=$ROW["ITEM_NAME"];
   $ITEM_ID="S".$ROW["ITEM_ID"];
   $title[$ITEM_NAME]=$ITEM_ID;
}

$title+=array("姓名"=>"USER_NAME");

$ROW_COUNT = 0;
$data=file_get_contents($CSV_FILE);
$lines=CSV2Array($data, $title,",");
if(!$data)
{
   Message("错误","打开文件错误!");
   Button_Back();
   exit;
}

$query = "DELETE FROM `SAL_DATA` WHERE `FLOW_ID` = '$FLOW_ID'";
$cursor= exequery($connection,$query);

foreach($lines as $line)
{
   $STR_VALUE="";
   $STR_KEY="";
   
   foreach ($line as $key => $value) 
   {
      if ($key!="USER_NAME")
      {
		 $STR_KEY.=$key.",";
		 $STR_VALUE.="'$value'".",";
      }
	  else
	  {
	     $USER_NAME=$value;
	  }
   }
 
   if (substr($STR_KEY,-1)==",")
      $STR_KEY=substr($STR_KEY,0,-1);
   if (substr($STR_VALUE,-1)==",")
      $STR_VALUE=substr($STR_VALUE,0,-1);
	  
   $query="insert into SAL_DATA(FLOW_ID,USER_ID,";
   $query1 = "SELECT USER_ID from USER where USER_NAME='$USER_NAME'";
   $cursor1= exequery($connection,$query1);
   if(!$ROW=mysql_fetch_array($cursor1))
   {
      echo "<font color=#FF6633><b>员工".$USER_NAME."尚未在OA系统中注册!!</b></font><br>";
      continue;
   }
   
   $query.=$STR_KEY.") values ('$FLOW_ID','$ROW[0]',".$STR_VALUE.")";
   exequery($connection,$query);
   echo "员工".$USER_NAME."的工资导入完成!!<br>";
   $ROW_COUNT++;
}

if(file_exists($CSV_FILE))
   @unlink($CSV_FILE);

Message("","共".$ROW_COUNT."条数据导入!");
?>
<div align="center">
<input type="button" value="返回" class="BigButton" onClick="location='index.php';" title="返回">
</div>

</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?