java 实现excel中的数据导入到数据库的功能

java web项目导入excel获取数据,是实用频率非常高的功能,通过做了几个这样的功能之后,现将此功能总结出了,为了以后自己方便使用,也为大家实现此功能做一个参考.

项目框架

1,后台:spring+springmvc+mybatis

2,前台: bootstrap+jquery+ajax

3,项目管理:maven

说明.excel处理函数需要引入poi的jar包,在pom.xml引入一下代码

    <!-- POI -->
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>3.8</version>
        <exclusions>
            <exclusion>
                <artifactId>commons-codec</artifactId>
                <groupId>commons-codec</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.8</version>
    </dependency>

别的框架大体上也是可以的,只需稍微调整,如有问题,大家可留言讨论

实现的功能说明:将用户信息(姓名,性别,年龄)通过excel上传,并保存到数据库

具体代码如下

1,前台html代码

    <form enctype="multipart/form-data" id="batchUpload"  action="user/upload" method="post" class="form-horizontal">    
        <button class="btn btn-success btn-xs" id="uploadEventBtn" style="height:26px;"  type="button" >选择文件</button>
        <input type="file" name="file"  style="width:0px;height:0px;" id="uploadEventFile">
        <input id="uploadEventPath"  disabled="disabled"  type="text" >                                            
    </form>
    <button type="button" class="btn btn-success btn-sm"  οnclick="user.uploadBtn()" >上传</button>

前台页面效果

excel上传样式

excel内容展示

excel内容

2,JS代码

    var User = function(){
        this.init = function(){
            //模拟上传excel
             $("#uploadEventBtn").unbind("click").bind("click",function(){
                 $("#uploadEventFile").click();
             });
             $("#uploadEventFile").bind("change",function(){
                 $("#uploadEventPath").attr("value",$("#uploadEventFile").val());
             });
        };
        //点击上传按钮
        this.uploadBtn = function(){
            var uploadEventFile = $("#uploadEventFile").val();
            if(uploadEventFile == ''){
                alert("请选择excel,再上传");
            }else if(uploadEventFile.lastIndexOf(".xls")<0){//可判断以.xls和.xlsx结尾的excel
                alert("只能上传Excel文件");
            }else{
                var url =  '/user/upload/';
                var formData = new FormData($('form')[0]);
                user.sendAjaxRequest(url,'POST',formData);
            }
        };
        this.sendAjaxRequest = function(url,type,data){
            $.ajax({
                url : url,
                type : type,
                data : data,
                success : function(result) {
               &

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值