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 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘