这个是简单的测试返回的数据存到list里的,没有返回属性类;
返回list的数据是对应属性类的话需要它封装有的Java类,这里只是简单的测试并没有太多的东西需要到
Excel必要jar包:
前端:
<form id="importForm" action="${ctx}/rental/fcRentalRegistration/import" method="post" enctype="multipart/form-data"
class="form-search" style="padding-left:20px;text-align:center;" onsubmit="loading('正在导入,请稍等...');"><br/>
<input id="uploadFile" name="file" type="file" style="width:330px"/><br/><br/>
<input id="btnImportSubmit" class="btn btn-primary" type="submit" value=" 导 入 "/>
<a href="${ctx}/rental/fcRentalRegistration/import/template">下载模板</a>
</form>
后端:
@RequestMapping(value = "import", method=RequestMethod.POST)
public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
if(file.getSize() == 0){
addMessage(redirectAttributes, "导入信息失败! 文件数据为空" );
return "redirect:"+Global.getAdminPath()+"/housepro/fcHouseProMain/?repage";
}
StringBuilder failureMsg = new StringBuilder();
CommonsMultipartFile cFile = (CommonsMultipartFile) file;
DiskFileItem fileItem = (DiskFileItem) cFile.getFileItem();
Workbook workbook;
try {
workbook = WorkbookFactory.create(fileItem.getInputStream());
//获取第一张Sheet表
Sheet sheet = workbook.getSheetAt(0);
//Sheet的行是从0开始
int endRow = sheet.getLastRowNum() +1; //获取需要读取内容的结束行标
List<ArrayList<String>> lists = new ArrayList<ArrayList<String>>();
if (endRow > 1) {
for (int i = 2; i < endRow; i++) {//这里从第2开始下标是因为我Excel文件标题有两行
ArrayList<String> array = new ArrayList<String>();
Row row = sheet.getRow(i);
for(Cell cell: row){
cell.setCellType(Cell.CELL_TYPE_STRING);//根据不同类型转化成字符串
String value = cell.getStringCellValue();//如果上面转化不行就直接 ""+cell.getStringCellValue();
array.add(value);
}
lists.add(array);
}
}
return "redirect:"+Global.getAdminPath()+"/housepro/fcHouseProMain/?repage";//返回路径
} catch (InvalidFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return "";//返回路径
}