标签:
1.head引用
<script type="text/javascript" src="<%=request.getContextPath() %>/js/ajaxfileupload.js"></script>
2.重写下面方法
function ajaxFileUpload() { var elementIds=["flag"]; //flag为id、name属性名 $.ajaxFileUpload({ url: ‘importSemzzTongjiExcel.action‘, type: ‘post‘, secureuri: false, //一般设置为false fileElementId: ‘file‘, // 上传文件的id、name属性名 dataType: ‘text‘, //返回值类型,一般设置为json、application/json elementIds: elementIds, //传递参数到服务器 success: function(data, status){ alert("上传成功"); }, error: function(data, status, e){ alert(e); } }); }
3.写jsp上传jsp代码
<form id ="importExcel" action="importSemzzTongjiExcel.action" method="post" enctype="multipart/form-data"> <input type="file" name ="file" id ="file"> <input type="button" value="上传excel" onclick="ajaxFileUpload()"> </form>
4.对应的action代码
@Value("#{config[‘winFileUrl‘]}") private String winFileUrl; @Value("#{config[‘linFileUrl‘]}") private String linFileUrl;
/** * * @param excelFileName * @param month * @throws Exception */ @RequestMapping("importSemzzTongjiExcel") public String importSemzzTongjiExcel(String month,@RequestParam MultipartFile file) throws Exception{ if(month == null){ month =CalendarUtil.getShotDateToString(CalendarUtil.getLastDateOfMonth(new Date())); } System.out.println(System.getProperties().getProperty("os.name")); String fileUrl =""; try { String sysName =System.getProperties().getProperty("os.name"); if(sysName.startsWith("Windows")){ fileUrl =winFileUrl+file.getOriginalFilename(); }else{ fileUrl=linFileUrl+file.getOriginalFilename(); } file.transferTo(new File(fileUrl)); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } List<Map<String,Object>> list =service.getsemzzList(month,null,null); InputStream input; try { input = new FileInputStream(fileUrl); POIFSFileSystem fs = new POIFSFileSystem(input); HSSFWorkbook wb = new HSSFWorkbook(fs); HSSFSheet sheet = wb.getSheetAt(0); int rowNum = sheet.getLastRowNum(); for(int i=1;i<=rowNum;i++){ HSSFRow row= sheet.getRow(i); HSSFCell cell1 =row.getCell(0); String userName =null; if(cell1!=null){ userName =cell1.getStringCellValue(); for (Map<String,Object> map : list) { String userNameSql =(String) map.get("username"); if(userName.equals(userNameSql)){ Double xindanaccountstc = Double.parseDouble(map.get("xindanaccountstc").toString()); Double xudanaccountstc = (Double.parseDouble(map.get("xudanaccountstc").toString())); Double tichengheji =xindanaccountstc+xudanaccountstc; String gangwei = null; String position = null; Double dyrealjixiaoxishu =0D; HSSFCell cell2 =row.getCell(1); if(cell2!=null){ gangwei=cell2.getStringCellValue(); } HSSFCell cell3 =row.getCell(2); if(cell3!=null){ position=cell3.getStringCellValue(); } HSSFCell cell4 =row.getCell(3); if(cell4!=null){ dyrealjixiaoxishu =cell4.getNumericCellValue(); } service.updateData(userNameSql, month, gangwei, position, dyrealjixiaoxishu,tichengheji); break; } } } } // Iterate over each row in the sheet } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("end..."); return null; }
标签:
原文地址:http://www.cnblogs.com/w8104254/p/5504187.html