du6029076877 2018-07-09 05:17
浏览 58

无法使用php从excel文件中读取每个列和行中的记录

I have a form where user upload a excel file when click on upload submit button then file is move into folder but its cannot read records of each rows and columns in excel files. Here is form code

<form id="TypeValidation" method="" enctype="multipart/form-data">
    <div>
        <input type="file" name="donor_attachment" id="donor_attachment" />
        <button type="submit" name="donor_file" id="donor_file" value="donor_file" class="btn btn-success btn-round fileinput-exists">Upload</button>
    </div>
</form>

file is submit through ajax

$("#TypeValidation").on('submit',(function(e) 
 { 
  var fileUploadID = $(this).find('button[name="donor_file"]').val();
  e.preventDefault();   
   $.ajax({
        url: "fn_dsr_wizard_submit.php?submitid="+fileUploadID,
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        dataType: "html",    
        success: function (result) { 
        alert(result);
        location.reload();
        if(result=='1'){
            location.replace("donors_dashboard.php");
        }else {
            location.replace("donors_dashboard.php");               
        }
        }
    }); 

    }

    ));

here is fn_dsr_wizard_sunbmit.php code

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

$userid = $_SESSION['Cont_ID'];
$company_id = $_SESSION['Company_ID'];

//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);

$fileextension = explode(".",$donor_attachment);
if($fileextension[1]=="xls") {

$data = new Spreadsheet_Excel_Reader();
$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=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
    { 
        $html.="<tr>";
        for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
        {
            $html.="<td>";
            $html.=$data->sheets[$i][cells][$j][$k];
            $html.="</td>";
        }
        $eid = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][1]);
        $name = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][2]);
        $email = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][3]);
        $dob = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][4]);
        //$query = "insert into excel(eid,name,email,dob) values('".$eid."','".$name."','".$email."','".$dob."')";

        //mysqli_query($connection,$query);
        $html.="</tr>";
    }
}

}

 $html.="</table>";
 echo $eid;
 echo $name;
 echo $email;
 echo $dob;
} 
}
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;
}

 //}
}

Issue is that recods are not read from each columns and rows in excel and when add this line $excel->read('$donor_attachment'); after this line $data = new Spreadsheet_Excel_Reader(); then show me alert error of file is not readable. I want that records are read from excel file

  • 写回答

1条回答 默认 最新

  • dtttlua7165 2018-07-09 06:01
    关注

    After I modified my code then read a records from excel file

    if(isset($_REQUEST['submitid']) && $_REQUEST['submitid']=='donor_file'){
    global $con;
    
    $userid = $_SESSION['Cont_ID'];
    $company_id = $_SESSION['Company_ID'];
    
    //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>";
            for($k=3;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
            {
                $html.="<td>";
                $html.=$data->sheets[$i][cells][$j][$k];
                $html.="</td>";
            }
            $eid = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][1]);
            $name = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][2]);
            $email = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][3]);
            $dob = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][4]);
            //$query = "insert into excel(eid,name,email,dob) values('".$eid."','".$name."','".$email."','".$dob."')";
    
            //mysqli_query($connection,$query);
            $html.="</tr>";
        }
      }
     }
    
    $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;
    }   
    //}
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)