请帮我看一下附加的excel文件上传。当我附加XLS格式时,我的PHPExcel阅读器无法正常工作。你觉得我在哪里错过了吗?
HTML
<form role="form" id="myForm" class="add_customer_form" enctype="multipart/form-data">
<label for="fileUpload">Upload File *</label>
<input type="file" id="files" name="file[]" class="form-control"
accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, text/plain, application/vnd.ms-excel"
multiple required>
</form>
AJAX
// USAGE:$(&#34;#form&#34;)。serializefiles();
(function($) {
$.fn.serializefiles = function() {
var obj = $(this);
/* ADD FILE TO PARAM AJAX */
var formData = new FormData();
$.each($(obj).find("input[type='file']"), function(i, tag) {
$.each($(tag)[0].files, function(i, file) {
formData.append(tag.name, file);
});
});
var params = $(obj).serializeArray();
$.each(params, function (i, val) {
formData.append(val.name, val.value);
});
return formData;
};
})(jQuery);
$.ajax({
url: base_url+'customer/customer_add',
type: 'POST',
data: values,
cache: false,
contentType: false,
processData: false,
success: function(data){
console.log(data);
}
});
PHP
foreach($_FILES["file"]['name'] as $file => $fname) {
$file_path = $_FILES["file"]["tmp_name"][$file];
$extension = pathinfo($fname);
if($extension['extension'] == 'xls') {
$objReader = PHPExcel_IOFactory::createReader('Excel5');
}
else {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
}
$inputFileType = PHPExcel_IOFactory::identify($file_path);
$objPHPExcel = $objReader->load($file_path);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
print_r($cell_collection); die();
}
DISPLAY ERROR
答案 0 :(得分:0)
不要信任文件扩展名.... xls
文件不是本机Excel BIFF格式文件,无论它可能声称什么扩展名。找到.xls
作为csv文件或包含html标记的文件的扩展名是很常见的。
使用IO Factory的identify()
方法告诉您要使用的读者;或者只是简单地使用IO Factory load()
方法选择正确的阅读器并为您加载文件。