首页 > 其他 > 详细

poi 导入Excel --实战代码(导入一行过一排排那种/工具类导入/复制代码用)

时间:2020-11-18 19:35:02      阅读:39      评论:0      收藏:0      [点我收藏+]

html代码

<a class="btn btn-primary fa fa-import" href="javascript:void(0);" id="importExcel"><span>导入excel</span></a>

js代码

<script type="text/javascript">
    $(function() {
        //导入基础数据
        $("#importExcel").click(function() {
                            var url = __ctx+ ‘/platform/xxx/xxx/importExcel.htm‘;
                            importExcel(url);//导入数据
                        });

    });

    /**
     * 产生导入对话框,并打开
     * @param  actionUrl  请求的Url
     */
    function importExcel(actionUrl) {
        var f = document.createElement("form");
        var i = document.createElement("input");
        f.name = ‘myFrom‘;
        f.action = actionUrl;
        f.method = "post";
        i.type = "file";
        i.name = "xmlFile";
        $(i).css("display", "none");
        $(i).attr("onchange", "importData(‘" + actionUrl + "‘)");
        //$(i).attr("accept", ".xls");
        $(i).attr("accept", [ ‘.xls‘, ‘.xlsx‘ ]);
        $(f).attr("enctype", "multipart/form-data");
        f.appendChild(i);
        document.body.appendChild(f);
        i.click();
    }

    /**
     *提交表单
     */
    function importData(actionUrl) {
        //同步提交
        //$("form[name=‘myFrom‘]").submit();

        //异步提交
        var nmiframe = document.forms[‘myFrom‘];
        var formData = new FormData(nmiframe);
        $.ajax({
            url : actionUrl,
            type : ‘POST‘,
            data : formData,
            async : false,
            cache : false,
            contentType : false,
            processData : false,
            success : function(returndata) {
                alert(returndata);
                window.location.reload(true);//重新加载页面

            },
            error : function(returndata) {
                alert(returndata);
            }
        });
    }
</script>

java代码

 /*
     * 具体导入操作
     * 
     * 
     */
    public void importExcelHandle(MultipartHttpServletRequest request, Workbook wb) throws Exception {
        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        // 读取了模板内所有sheet内容
        HSSFSheet sheet = (HSSFSheet) wb.getSheetAt(0);
        HSSFCell cell = null;

        int physicalNumberOfRows = sheet.getLastRowNum();//指物理行数,并不是下标,即行数从1开始

      //String[] headers = {"序号","单位","机组","容量","开始","结束","工期","类型","发电类型"};
        String[] headersVar = {"xh","dw","jz","rl","ks","js","gq","lx","zt"};
        
        //把sheet内容转换成list
        
        List<Jzjxjh> list = new ArrayList<Jzjxjh>();
        // 遍历每一行,从第三行开始
        for (int i = 2; i <= physicalNumberOfRows; i++) {
            Jzjxjh  jzjxjh = new Jzjxjh();
            Object object = null;
            // 遍历每一列
            for (int j = 0; j < headersVar.length; j++) {
                cell = sheet.getRow(i).getCell(j);
                cell.setCellType(Cell.CELL_TYPE_STRING);
                String colVal = cell.getStringCellValue();

                if (StringUtil.isNotEmpty(colVal)) {
                     object = ReflectUtil.setValueBySetter(headersVar[j], jzjxjh, colVal,"yyyy/MM/dd");
                }
            }
            Jzjxjh  e = (Jzjxjh) object;
            String id = UniqueIdUtil.getId();
            e.setId(id);
            e.setMyId(id);
            list.add(e);
        }

        //业务处理插入数据库
        for(Jzjxjh e:list){
             
        }
        
    }
其中 ReflectUtil工具类可到本博客util标签查找,注意下 上面代码表头和java类属性要一一对应
导入的excel样例

技术分享图片

 

poi 导入Excel --实战代码(导入一行过一排排那种/工具类导入/复制代码用)

原文:https://www.cnblogs.com/rdchen/p/14001373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!