[英]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.