问题:页面提交的Excel 文件 解析?
解决:
还是看看代码来的快,
附上代码
@RequestMapping(value="/import")
@ResponseBody
public void importRecovery(HttpServletRequest request,
@RequestParam MultipartFile file,
HttpServletResponse response) throws Exception{
InputStream in =null;
in = file.getInputStream();
String fileName = file.getOriginalFilename();
CommonsMultipartFile cf= (CommonsMultipartFile)file;
DiskFileItem fi = (DiskFileItem)cf.getFileItem();
File f = fi.getStoreLocation();
in = new FileInputStream(f);
Workbook work = new ImportExcelUtil().getWorkbook(in,fileName);
Sheet sheet = work.getSheetAt(0);
Row row = null;
int rowNum = 1; // 计数
Row row1 = sheet.getRow(rowNum); // 获得第一行看是否有值
Cell snCode = null;
if(!"".equals(row1) && null!=row1){
snCode = sheet.getRow(rowNum).getCell((short) 1);
}
// 这里采用 while 循环加行 判断下一行是否有值 获得总行数
while (!"".equals(snCode) && null!=snCode ) {
rowNum++;//总行数
row = sheet.getRow(rowNum);
if (row == null) {
break;
}
if ((row.getCell((short) 1) == null)) {
break;
}
}
rowNum--; // 此时就是excel 文件的总行数
// 这里就可以根据 总行数 进行解析了
for(int i ; i<= rowNum;i++){
row = sheet.getRow(i);
String cell =Input.getString(row.getCell(1));
String cell1 = Input.getString(row.getCell(2));
String cell2=Input.getString(row.getCell(3));
// 获得数据处理。在此不做列出
}
}
关键的列出来了,省略了一部分,
算是做一个记录吧,错误之处大牛指正。