页面组件:
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