方式与在struts1下面一样,用一个隐藏的iframe页面来负责提交文件,提交的路径和action都由struts2提供,如下:
<form action="importExcel" method="post" enctype="multipart/form-data" target="hidden_frame">
action类需要继承某一些接口,以能够调用request和response
public class OrderDetailAction extends ActionSupport implements SessionAware, ServletResponseAware
action中的方法则如下:
File tempPathFile;
// 默认路径
String uploadTo = "D:\\";
// 支持的文件类型
String[] errorType = { ".xls" };
// 格式化日期
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
public String uploadUsersExcel() throws Exception
{
User user = new User();
ActionContext ct= ActionContext.getContext();
HttpServletRequest request1=
(HttpServletRequest)ct.get(ServletActionContext. HTTP_REQUEST );
System.out.println(importExcel.getName());
try {
FileInputStream is = new FileInputStream(importExcel);
HSSFWorkbook wbs = new HSSFWorkbook(is);
HSSFSheet childSheet = wbs.getSheetAt(0);
// System.out.println(childSheet.getPhysicalNumberOfRows());
System.out.println("有行数" + (childSheet.getLastRowNum()+1));
for (int j = 1; j < childSheet.getLastRowNum()+1; j++) {
HSSFRow row = childSheet.getRow(j);
System.out.println(row.getPhysicalNumberOfCells());
System.out.println("有列数" + row.getLastCellNum());
if (null != row) {
HSSFCell cell = row.getCell((short) 0);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING: // 字符串
user.setName(cell.getStringCellValue());
user.setUserName(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println("为空");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
} else {
System.out.print("- ");
}
cell = row.getCell((short) 1);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING: // 字符串
user.setTitle(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println("为空");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
} else {
System.out.print("- ");
}
cell = row.getCell((short) 2);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING: // 字符串
user.setPhone(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println("为空");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
} else {
System.out.print("- ");
}
cell = row.getCell((short) 3);
if (null != cell) {
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING: // 字符串
user.setEmail(cell.getStringCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK: // 空值
System.out.println("为空");
break;
case HSSFCell.CELL_TYPE_ERROR: // 故障
System.out.println(" ");
break;
default:
System.out.print("未知类型 ");
break;
}
} else {
System.out.print("- ");
}
}
user.setOrderId(orderIdForExcel);
orderDetailService.addNewUser(user);
}
System.out.println();
//这个地方尤为重要,调用页面上的回调函数
String processParentFunctionStr="parent.backCallUpload()";
PrintWriter writer=response.getWriter();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
writer.write("<script>"+processParentFunctionStr+"</script>");
} catch (Exception e) {
String processParentFunctionStr="parent.backCallUploadAlert()";
PrintWriter writer=response.getWriter();
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
writer.write("<script>"+processParentFunctionStr+"</script>");
e.printStackTrace();
}
return null;
}
这个方法负责的是接收上传上来的excel文件,并读出其中的内容,调用数据库方法以存储。由于代码未优化,故显得比较冗余而且乱。
这里的回调方法相对简单一些,仅仅是关闭div和翻到最后一页而已。