thinkphp3.1.3 +bootstrap-fileinput +phpExcel 实现excel 导入

bootstrap-fileinput下载


导入样式跟js

<link type="text/css" rel="stylesheet" href="bootstrap-fileinput/css/fileinput.css" />

<script type="text/javascript" src="bootstrap-fileinput/js/fileinput.js"></script>

<script type="text/javascript" src="bootstrap-fileinput/js/fileinput_locale_zh.js"></script>

html

<div class="panel-body">

<form enctype="multipart/form-data">

<div class="file-loading">
<input id="file-4" class="file-4" name="initUpload" type="file" multiple accept=".csv,.xls,.xlsx">
</div>

</form>

<script>
$(document).on('ready', function() {
// the file input
var $el4 = $('#file-4'), initPlugin = function() {
$el4.fileinput({previewClass:''});
};

// initialize plugin
initPlugin();

// `disable` and `enable` methods
$(".btn-disable").on('click', function() {
var $btn = $(this);
if (!$el4.data('fileinput')) {
initPlugin();
$el4.trigger('change');
}
if ($el4.attr('disabled')) {
$el4.fileinput('enable');
$btn.html('Disable').removeClass('btn-primary').addClass('btn-secondary');
} else {
$el4.fileinput('disable');
$btn.html('Enable').removeClass('btn-secondary').addClass('btn-primary');
}
});

// `destroy` method
$(".btn-destroy").on('click', function() {
if ($el4.data('fileinput')) {
$el4.fileinput('destroy');
}
});

// recreate plugin after destroy
$(".btn-recreate").on('click', function() {
if ($el4.data('fileinput')) {
return;
}
initPlugin();
if ($el4.val()) {
$el4.trigger('change');
}
});

// refresh plugin with new options
$(".btn-refresh").on('click', function() {
$el4.fileinput('refresh', {previewClass:'bg-info'});
if ($el4.val()) {
$el4.trigger('change');
}
});
});
</script>
<script>

        $("#file-4").fileinput({
            uploadUrl:"{:U('zhongtong')}",//上传路径
            uploadAsync:false,//是否异步传输
            maxFileCount:1,//最大文件上传数量
            language: "zh",//设置语言
            showUpload: false,               //是否显示上传按钮
            showRemove: true,               //是否显示移除按钮
            showPreview : true,             //是否显示预览按钮
            allowedFileExtensions: ["csv","xls", "xlsx"],
            browseClass: "btn btn-primary", //按钮样式
            dropZoneEnabled: true,         //是否显示拖拽区域
        }) .on('fileuploaded', function (event, data, previewId, index) {
        if(data.response.res== 1){

 
        }
 
        });


</script>
后台

import('ORG.Net.UploadFile');
    $config=array(
        'allowExts'=>array('csv','xlsx','xls'),
        'savePath'=>'./uploads/excel/',
        'saveRule'=>'time',
    );
    $upload = new UploadFile($config);

    if (!$upload->upload()) {
        $this->error($upload->getErrorMsg());
    } else {
        $info = $upload->getUploadFileInfo();
    }
    header("Content-type: text/html; charset=utf-8");
    import("@.Class.PHPExcel");

    $file_name=$info[0]['savepath'].$info[0]['savename'];

import("@.Class.PHPExcelReader");
$excel = new Spreadsheet_Excel_Reader($file_name, true, "UTF-8");
$sheet = $excel->sheets[0]["cells"];// sheet 为获取到excel里的内容
//$file_name 为你上传的文件名


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值