[英]Cannot read records in each columns and rows from excel file using php
我有一個表格,當用戶單擊“上傳提交”按鈕時,用戶上傳了一個excel文件,然后該文件被移到了文件夾中,但是它無法讀取excel文件中每一行和每一列的記錄。 這是表格代碼
<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>
文件是通過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");
}
}
});
}
));
這是fn_dsr_wizard_sunbmit.php代碼
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;
}
//}
}
問題是,不能從excel的每一列和每一行中讀取Recod,並且在添加此行時$excel->read('$donor_attachment');
在這行之后$data = new Spreadsheet_Excel_Reader();
然后告訴我該文件的警報錯誤不可讀。 我希望從Excel文件中讀取記錄
修改代碼后,然后從excel文件中讀取記錄
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;
}
//}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.