使用Spreadsheet_Excel_Reader php从excel文件读取空单元格

时间:2018-07-09 12:58:59

标签: php excel

我想读取一个空单元格,但无法从excel文件读取,这是我的代码

if(isset($_REQUEST['submitid']) && $_REQUEST['submitid']=='donor_file'){
global $con;

$userid = $_SESSION['Cont_ID'];
$company_id = $_SESSION['Company_ID'];
$cdate = date('Y-m-d h:i:s');
$status = 1;

//Donor File Attachment
$F_filePath="../../uploads/ds_information/donor/";
$_FILES['donor_attachment']['name'];
if ($_FILES['donor_attachment']['name']!=""){
$donor_attachment = date('dmyhis')."_".$userid."_".$company_id."_".$_FILES['donor_attachment']['name'];
move_uploaded_file($_FILES['donor_attachment']['tmp_name'], $F_filePath.$donor_attachment);

$filepath = "../../uploads/ds_information/donor/".$donor_attachment;
$fileextension = explode(".",$donor_attachment);
if($fileextension[1]=="xls") {

$data = new Spreadsheet_Excel_Reader();
$data->read($filepath); 
$html="<table border='1'>";

 for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
  { 

    if(count($data->sheets[$i][cells])>0) // checking sheet not empty
  {
    for($j=3;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
    { 
        $html.="<tr>"; $DataArray; $index=1;

        for($k=2;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
        {
            //$kcount = count($data->sheets[$i][cells][$j]);
            //$html.="<td>";
            //$html.=$data->sheets[$i][cells][$j][$k];


            $DataArray[$index]=$data->sheets[$i][cells][$j][$k];

            //Insert Personal record
            if(sizeof($DataArray)==11){
                //echo "<pre>";
                //print_r($DataArray);
                //echo "</pre>";


            //Get gender value
            $gender_where = "status=1 and name like '".$DataArray[6]."'";
            $genderID = "id";
            $Row_gender = selectSRow($genderID,'tbl_gender',$gender_where,'','','');

            //Get martialstatus value
            $martial_where = "status=1 and name like '".$DataArray[7]."'";
            $martialID = "id";
            $Row_martial = selectSRow($martialID,'tbl_marital_status',$martial_where,'','','');

            //Get nationality value
            $nationality_where = "status=1 and name like '".$DataArray[8]."'";
            $nationalityID = "id";
            $Row_nationality = selectSRow($nationalityID,'tbl_nationality',$nationality_where,'','','');

            //Get Racial or Ethnic Origin value
            $rac_where = "status=1 and name like '".$DataArray[9]."'";
            $racID = "id";
            $Row_rac = selectSRow($racID,'tbl_racial',$rac_where,'','','');

            //Get religion value
            $religion_where = "status=1 and name like '".$DataArray[10]."'";
            $religionID = "id";
            $Row_religion = selectSRow($religionID,'tbl_religions',$religion_where,'','','');


$insert_data = array("date_of_birth"=>$DataArray[2],"first_name"=>Encrypt($DataArray[3]),
"middlename"=>Encrypt($DataArray[4]),"last_name"=>Encrypt($DataArray[5]),"gender"=>$Row_gender["id"],
"marital_status"=>$Row_martial["id"],"nationality"=>$Row_nationality["id"],
"rac_ethnic_orig"=>$Row_rac["id"],"religion_beliefs"=>$Row_religion["id"],"cdate"=>$cdate,"status"=>$status,
"userid"=>$userid,"company_id"=>$company_id);

$table =  z_personal_identify;
$PerInfo_InsertID = InsertDB($insert_data, $table);
            }
            //echo sizeof($DataArray);
            if(sizeof($DataArray)==34){
                echo "<pre>";
                print_r($DataArray);
                echo "</pre>";


            //Get county value
            $pcounty_where = "status=1 and name like '%".$DataArray[28]."%'";
            $pcountyID = "id";
            $Row_pcounty = selectSRow($pcountyID,'tbl_country',$pcounty_where,'','','');

            //Get county value
            $ocounty_where = "status=1 and name like '%".$DataArray[36]."%'";
            $ocountyID = "id";
            $Row_ocounty = selectSRow($ocountyID,'tbl_country',$ocounty_where,'','','');


//Insert Contact Information Record
$insert_contact = array("phone"=>Encrypt($DataArray[12]),"mobile"=>Encrypt($DataArray[13]),
"fax"=>Encrypt($DataArray[14]),"personal_email"=>Encrypt($DataArray[15]),
"official_email"=>Encrypt($DataArray[16]),"next_of_kin"=>Encrypt($DataArray[17]),
"nok_phone"=>Encrypt($DataArray[18]),"nok_mobile"=>Encrypt($DataArray[19]),
"nok_fax"=>Encrypt($DataArray[20]),"p_postcode"=>Encrypt($DataArray[21]),
"p_address"=>Encrypt($DataArray[22]),"p_suit_no"=>Encrypt($DataArray[23]),
"p_floor_no"=>Encrypt($DataArray[24]),"p_building"=>Encrypt($DataArray[25]),
"p_location"=>Encrypt($DataArray[26]),"p_city"=>Encrypt($DataArray[27]),
"p_county"=>$Row_pcounty["id"],"o_postcode"=>Encrypt($DataArray[29]),"o_address"=>Encrypt($DataArray[30]),
"o_suit_no"=>Encrypt($DataArray[31]),"o_floor_no"=>Encrypt($DataArray[32]),"o_building"=>Encrypt($DataArray[33]),
"o_location"=>Encrypt($DataArray[34]),"o_city"=>Encrypt($DataArray[35]),
"o_county"=>$Row_ocounty["id"],"cdate"=>$cdate,"status"=>$status,"userid"=>$userid,"company_id"=>$company_id);
$table =  z_contact_info;
$ContactInfo_InsertID = InsertDB($insert_contact, $table);
            }
            $index++;               
        }
        $html.="</tr>";
    }
}

}
 //echo $kcount;

 $html.="</table>";
 //echo $html;
} 
}
else {$donor_attachment=$_SESSION['donor_attachment']; unset($_SESSION['donor_attachment']);}

if($_FILES['donor_attachment']['name'] !=''){
    $_SESSION['fileInsert']="donor_file_success_msg";
    echo 1;
}
else{
    $_SESSION['filenotInsert']="donor_file_error_msg";
    echo 0;
}
//}
}

我希望在获取空单元格时将该单元格替换为空白以发送数据库值。当我回显此echo sizeof($DataArray);时,然后返回其大小为34的单元格,其中没有空单元格。有没有办法从excel文件中获取空单元格并替换blcnk以便将值发送到数据库。

0 个答案:

没有答案