[英]How to import excel file using phpexcel and ajax?
我在使用PHPExcel導入excel文件時遇到問題,當我嘗試導入時,出現致命錯誤:無法打開'filename.xls',文件不存在。 我該如何解決? 到目前為止,這是我的代碼。
javascript / Ajax上傳文件:
function load_file(id,url,type,data,json,callback) {
var xmlhttp;
var fdata = new FormData();
fdata.append('SelectedFile', data);
(window.XMLHttpRequest)?xmlhttp = new XMLHttpRequest():xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange=function() {
if(xmlhttp.readyState==4 && xmlhttp.status==200) {
((id != "")?(document.getElementById(id).innerHTML = xmlhttp.responseText):(((json == true)?(callback(JSON.parse(xmlhttp.responseText))):(callback(xmlhttp.responseText)))));
}
}
xmlhttp.open(type,url,true);
if(type == "POST") {
xmlhttp.send(fdata);
} else {
xmlhttp.send();
}
}
load_file("","../../../phpscript/management/import_excel.php","POST",subNavigationAidToolFileInput.files[0],false,function(data) {
alert(data);
});
PHP:
/* Database Connection */
include_once "../global/db_connect.php";
require "../../resources/widgets/PHPExcel_1.8.0_doc/Classes/PHPExcel.php";
require_once "../../resources/widgets/PHPExcel_1.8.0_doc/Classes/PHPExcel/IOFactory.php";
$Connection = new connection();
$objPHPExcel = PHPExcel_IOFactory::load($_FILES['SelectedFile']['name']);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow(); // e.g. 10
$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
}
echo $_FILES['SelectedFile']['name'];
$ _FILES ['SelectedFile'] ['name']是對文件名的引用,而不是文件本身。
php將上傳的文件存儲到php.ini中定義的臨時位置
然后,您可以將其移動並保存到永久位置,這是我建議您在嘗試通過PHPExcel訪問/解析它之前執行的操作。
這里是參考:
http://php.net/manual/zh/features.file-upload.post-method.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.