页面组件:

    
     
上传

formdata可以提交form表单里的file文件。

页面代码:

function uploadfile(){    var formData = new FormData($( "#searchForm" )[0]);      $.ajax({         url : '<%=basePath%>'+"dbnhpz/shangchuan_tsdfDaoru.action",           type: 'post',         data: formData,         async: false,         cache: false,         contentType: false,         processData: false,         success:function(result){         if (result.info == '导入成功') {            alert(result.info);            document.getElementById("tables").innerHTML = result.state;          } else {            alert(result.info);                     }      }     });    }

workbook类读取文件,创建List<Map<String, String>>集合表示整张表格,每行的数据存入一个map集合,然后map集合存入list。

后台代码:

public String tsdfDaoru(){        returnMessage = new ReturnMessage();        returnMessage.setInfo("导入成功");        try {            InputStream in = new FileInputStream(file);//构建输入流            Workbook book = null;            String extension = fileFileName.lastIndexOf(".") == -1 ? "" : fileFileName                    .substring(fileFileName.lastIndexOf(".") + 1);            if ("xls".equals(extension)) {                book = new HSSFWorkbook(in);            } else if ("xlsx".equals(extension)) {                book = new XSSFWorkbook(in); //excel2007版本的             } else {                returnMessage.setInfo("不支持的文件类型");                return SUCCESS;            }            Sheet sheet = book.getSheetAt(0);//得到表格            List
> ls = new ArrayList
>();// 读数据            Cell c; //接收每一列。            int type; //用于接收单元格的所属类型。             String k = ""; //用于接收每个单元格的数据。             Map
 str=null; //用于接收每行的数据。             int coloumNum=sheet.getRow(0).getPhysicalNumberOfCells();//获得总列数            int rowNum=sheet.getLastRowNum()+1;//获得总行数            int bt=3;//表头行数            //空模板            if(rowNum<=bt){                returnMessage.setInfo("空模板,请录入数据");                return SUCCESS;            }            for (int row = bt; row < rowNum; row++) {                int j = 0; //用于设置主键的。                 for(int i=0;i
();//产生列对象。                     c = sheet.getRow(row).getCell(i);//得到单元格能耗比例信息                     if(c!=null){                        type = c.getCellType(); //得到单元格数据类型                         switch (type) { //判断数据类型                         case Cell.CELL_TYPE_BLANK:                            k = "";                            break;                        case Cell.CELL_TYPE_NUMERIC:                            c.setCellType(Cell.CELL_TYPE_STRING);                            k = c.getStringCellValue();                            break;                        case Cell.CELL_TYPE_STRING:                            k = c.getStringCellValue();                            break;                        case Cell.CELL_TYPE_FORMULA:                            c.setCellType(Cell.CELL_TYPE_STRING);                            k = c.getStringCellValue();                            break;                        default:                            break;                        }                        }else{                        k="";                    }                    k=k.replace(" ","");                    str.put(String.valueOf((j++)),k); //单元格数据存入map集合                    if (!str.isEmpty()) { //判断是不是为空                         ls.add(str);                    }                }            }                System.out.println(ls);        } catch (Exception e) {            e.printStackTrace();        }        return SUCCESS;    }