SpringMvc上传excel(注解和⾮注解两种⽅式)
1、第⼀种⽅式:
A:JSP页⾯:
<form name="importForm" action="${ctx }/service/userService/BatchImport.do"method="post" enctype="multipart/form-data">
请选择上传所需⽂件:<input type="file" id="theFile" name="theFile" /><br/>
<input  type="submit"value="提交"/>
</form>
B: Controller 层
@Controller
@RequestMapping("/service/userService")
public class LoadUserServiceAction  {
@Reference
UserHelper userHelper;
@Reference
ClassHelper classHelper;
@Reference
InstitutionHelper institutionHelper;
@RequestMapping("/BatchImport")
public void BatchImport(HttpServletRequest request, HttpServletResponse response)
throws Exception {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter pw = null;
request.setCharacterEncoding("utf-8");
int roleID = Parameter("role"));
int organID = Parameter("organ"));
//是否忽略警告
boolean ignoreWarning = Parameter("ignore"));
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile importFile = File("theFile");
try {
JSONObject json = new JSONObject();
json.put("needConfirm", false);
pw = Writer();
if(importFile == null){
json.put("error", "未读取到⽂件");
pw.String());
}else{
Workbook workbook = null;
String fileName = OriginalFilename();
if(StringUtils.isNotBlank(fileName)){
String[] strArr = fileName.split("\\.");
if(strArr != null && strArr.length > 0){
if("xls".equals(strArr[strArr.length - 1])){
workbook = new InputStream());
} else if("xlsx".equals(strArr[strArr.length - 1])){
workbook = new InputStream());
} else {
json.put("error", "⽆法读取的⽂件格式");
pw.String());
}
}
}
StringBuilder errorReason = new StringBuilder();
if(organID < 0){
if(organID < 0){
json.put("error", "读取不到正确的机构id");
pw.String());
}
List<Object[]> objsArrList = readExcel(workbook, organID, roleID);
boolean needConfirm = checkData(objsArrList, roleID, errorReason);
if(errorReason.length() > 0 &&
( (needConfirm && ! ignoreWarning) || !needConfirm )){
json.put("needConfirm", needConfirm);
json.put("error", String().replaceAll("\\[|]", ""));
pw.String());
}
userHelper.saveUserObjects(objsArrList);
pw.print("success");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(pw != null){
pw.flush();
pw.close();
}
}
}replaceall()
/**
* 检查数据
* @param objsArrList 对象数组
* @param errorReason 错误原因
*/
private boolean checkData(List<Object[]> objsArrList, int roleID, StringBuilder errorReason){
//这是省略了检验数据的代码。。。。
}
/**
* 读取excel并且形成对象
* @param workbook ⼯作簿对象
* @return
*/
private List<Object[]> readExcel(Workbook workbook, int organID, int roleID){
List<Object[]> objsArr = new ArrayList<Object[]>();
//得到⼯作簿开始解析数据
Sheet sheet = SheetAt(0);
//处理数据
}
}
2、采⽤注解的⽅式接受jsp传过来的⽂件
A: jsp 页⾯不变
B: Controller 层
其实和上⾯差不多 主要的区别就是在⽅法⾥添加了参数,该参数采⽤注解的⽅式,前提条件是要求参数名和jsp中的⽂件的name值必须⼀致,否则得到的是null;
具体代码:
@Controller
@RequestMapping("/service/userService")
public class LoadUserServiceAction  {
@Reference
UserHelper userHelper;
@Reference
ClassHelper classHelper;
@Reference
InstitutionHelper institutionHelper;
@RequestMapping("/BatchImport")
public void BatchImport(MultipartFile theFile,HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter pw = null;
request.setCharacterEncoding("utf-8");
int roleID = Parameter("role"));
int organID = Parameter("organ"));
//是否忽略警告
boolean ignoreWarning = Parameter("ignore"));
//theFile拿到的就是该⽂件,然后进⾏相应的操作
//和上⾯的⼀样
}
}
纯⼿⼯作业,转摘的时候请附上我的url。谢谢

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。