java 通过上传的表格批量处理

来源:互联网 发布:最好英语辅导软件 编辑:程序博客网 时间:2024/06/06 04:31
<div id="div_batchImportChannelru"><form id="form_batchImportChannelru" method="post" enctype="multipart/form-data"><table><tr><th>上传文件:</th><td><input name="file" type="file" value="上传文件"></td></tr><tr><th>第一个数据列(1234):</th><td><input type="text" name="chruChannelCellFirstColumn" class="easyui-numberbox" required="true" editable="true"style="width: 150px;"></td><th>第一个数据所在行(1234):</th><td><input type="text" name="chruChannelCellFirstRow" class="easyui-numberbox" required="true" editable="true"style="width: 150px;"></td></tr></table></form></div>
要注意表格中如果有数据并且小数位超过两位的话要处理一下,将表格上传到指定路径保存,然后将里面的数据存到list集合中 然后传到service层业务处理
 public String batchImportChannelru(MultipartHttpServletRequest fileRequest,    Integer chruChannelCellFirstColumn, Integer chruChannelCellFirstRow,HttpServletRequest request) {String operator = (String) request.getSession().getAttribute("userName");MultipartFile uploadFile = fileRequest.getFile("file");if (uploadFile.isEmpty()) {    return MessageUtil.operationFail("上传的文件不能为空");}String file = uploadFile.getOriginalFilename(); // 获取文件名String subFile=file.substring(0,file.length() - 4);String fileLast = file.substring(file.length() - 4, file.length()); // 获取文件格式最后4位if (!".xls".equals(fileLast)) {    return MessageUtil.operationFail("上传的文件不是xls格式,请另存为xls格式");}String fileName = subFile+"_"+new SimpleDateFormat("HHmmss").format(new Date())+fileLast;String folderPath = Path.CHRU_IMPORTCHANNELRU_PATH+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + "/";LoggerUtil.warn("==batchImportChannelru:批量导入保存文件路径=="+folderPath);boolean isCreateFolderOk = createFolder(folderPath);if (!isCreateFolderOk) {    LoggerUtil.warn("创建文件夹失败");    return MessageUtil.tryAgain();}OutputStream oos = null;String name=folderPath + fileName;try {name = new String(name.getBytes(), "UTF-8");} catch (UnsupportedEncodingException e1) {LoggerUtil.warn("=====加载文件名异常======");e1.printStackTrace();}File localFile = new File(name);try {    oos = new FileOutputStream(localFile);    oos.write(uploadFile.getBytes());} catch (FileNotFoundException e) {    LoggerUtil.warn("创建文件失败");    return MessageUtil.tryAgain();} catch (IOException e) {    return MessageUtil.tryAgain();} finally {    try {oos.close();    } catch (IOException e) {    LoggerUtil.warn("关闭流失败");    }}List<List<String>> chrus = new ArrayList<List<String>>();Workbook book = null;try {    book = Workbook.getWorkbook(localFile);    Sheet sheet = book.getSheet(0);// 获得第一个工作表对象    int columNum = sheet.getColumns(); // 得到列数    int rowNum = sheet.getRows(); // 得到行数    int chruLruidCellColumIndex = chruChannelCellFirstColumn - 1;    int chruLruidCellFirstRowIndex = chruChannelCellFirstRow - 1;    if (chruLruidCellColumIndex > columNum || chruLruidCellFirstRowIndex> rowNum ) {    return MessageUtil.operationFail("所填列数、行数超出文件范围");    }    for (int i = chruLruidCellFirstRowIndex; i < rowNum; i++) {    List<String> lists = new ArrayList<String>();    for (int j = chruLruidCellColumIndex; j < columNum; j++) {    String element ="";    if(j==8||j==10||j==12||j==13||j==15||j==16||j==18||j==20||j==22||j==24||j==26){    if(sheet.getCell(j, i).getType() != CellType.EMPTY){    NumberCell numberCell = (NumberCell)sheet.getCell(j, i);    element=numberCell.getValue()+"";    }else{    element="";    }    }else{    Cell chruLruidCell = sheet.getCell(j, i);    element = chruLruidCell.getContents();    }if(element!=null&&!"".equals("")){element =element.trim();}lists.add(element);}    chrus.add(lists);    }} catch (BiffException e) {    return MessageUtil.operationFail("非标准excel文件");} catch (IOException e) {    return MessageUtil.tryAgain();} finally {    book.close();}if(chrus.size()<=0 ||"".equals(chrus)){return MessageUtil.operationFail("未找到参数!");}    }

 private boolean createFolder(String folderPath) {File folder = new File(folderPath);if (folder.exists()) {    return true;}folder.mkdirs();return true;    }