<input type="file" οnchange="uploadDo()" id="uploadInput" style="display: none" />
js代码
*/
function uploadBtn() {
$('#uploadInput').click();
}
function uploadDo(){
var formData = new FormData();
formData.append("excelFile", document.getElementById("uploadInput").files[0]);
formData.append("order_id", {$order_id});
formData.append("group_id", {$group_id});
$.ajax({
url: '/home/order/group_offer_import',
type: "POST",
data: formData,
contentType: false, //必须false才会自动加上正确的Content-Type
processData: false, //必须false才会避开jQuery对 formdata 的默认处理
success:function(msg){
if (msg.result == 'success') {
new $.zui.Messager('导入成功,2秒后刷新页面', {type: 'success'}).show();
setTimeout(function(){window.location.reload();},2000);
} else {
new $.zui.Messager(msg.message, {type: 'danger'}).show();
}
}
})
}
PHP代码
$file = $_FILES['excelFile'];
$ext = pathinfo($_FILES['excelFile']['name'], PATHINFO_EXTENSION);
$order_id = I('post.order_id');
$group_id = I('post.group_id');
if (!in_array($ext, array('xls','xlsx'))) {
$this->returnError('上传格式错误');
}
if (empty($order_id)) {
$this->returnError('数据错误');
}
//读取excel数据
$filename = $file['tmp_name'];
//或者直接引入库文件
// include_once(IWEB_PATH.'core/util/phpexcel/PHPExcel.php');
// include_once(IWEB_PATH.'core/util/phpexcel/PHPExcel/IOFactory.php');
import("Org.Util.PHPExcel");
$objPHPExcel = new \PHPExcel();
$objPHPExcel = \PHPExcel_IOFactory::load($filename);
//
$objWorksheet = $objPHPExcel->getActiveSheet();
$array = $objWorksheet->toArray('', false, true, false);
//插入数据库